Class: FFMPEG::Transcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/ffmpeg/transcoder.rb

Overview

Transcodes media files with progress reporting

Examples:

Basic transcoding

transcoder = Transcoder.new(media, "/output.mp4")
transcoder.run do |progress|
  puts "#{progress}% complete"
end

With options

transcoder = Transcoder.new(media, "/output.webm",
  video_codec: "libvpx-vp9",
  audio_codec: "libopus",
  video_bitrate: "2M"
)
result = transcoder.run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media, output_path, **options) ⇒ Transcoder

Create a new Transcoder

Parameters:

  • media (Media)

    source media

  • output_path (String)

    output file path

  • options (Hash)

    transcoding options

Options Hash (**options):

  • :video_codec (String)

    video codec (default: libx264)

  • :audio_codec (String)

    audio codec (default: aac)

  • :resolution (String)

    output resolution (e.g., “1280x720”)

  • :frame_rate (String, Float)

    output frame rate

  • :video_bitrate (String)

    video bitrate (e.g., “2M”)

  • :audio_bitrate (String)

    audio bitrate (e.g., “128k”)

  • :seek (Float)

    start time in seconds

  • :duration (Float)

    duration in seconds

  • :copy_video (Boolean)

    copy video without re-encoding

  • :copy_audio (Boolean)

    copy audio without re-encoding

  • :no_audio (Boolean)

    strip audio

  • :video_filters (Array<String>)

    video filter chain

  • :audio_filters (Array<String>)

    audio filter chain

  • :format (String)

    output format

  • :custom (Array<String>)

    custom FFmpeg arguments

  • :threads (Integer)

    number of threads



50
51
52
53
54
# File 'lib/ffmpeg/transcoder.rb', line 50

def initialize(media, output_path, **options)
  @media = media
  @output_path = File.expand_path(output_path)
  @options = default_options.merge(options)
end

Instance Attribute Details

#mediaMedia (readonly)

Returns source media.

Returns:

  • (Media)

    source media



22
23
24
# File 'lib/ffmpeg/transcoder.rb', line 22

def media
  @media
end

#optionsHash (readonly)

Returns transcoding options.

Returns:

  • (Hash)

    transcoding options



28
29
30
# File 'lib/ffmpeg/transcoder.rb', line 28

def options
  @options
end

#output_pathString (readonly)

Returns output path.

Returns:

  • (String)

    output path



25
26
27
# File 'lib/ffmpeg/transcoder.rb', line 25

def output_path
  @output_path
end

Instance Method Details

#commandArray<String>

Get the FFmpeg command that will be executed

Returns:

  • (Array<String>)


80
81
82
# File 'lib/ffmpeg/transcoder.rb', line 80

def command
  build_command
end

#command_previewString

Preview the command as a string

Returns:

  • (String)


86
87
88
# File 'lib/ffmpeg/transcoder.rb', line 86

def command_preview
  build_command.join(" ")
end

#run {|Float| ... } ⇒ Media

Run the transcoding

Yields:

  • (Float)

    progress percentage (0-100)

Returns:

  • (Media)

    the output media

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ffmpeg/transcoder.rb', line 60

def run(&block)
  ensure_output_directory!

  cmd = build_command
  total_duration = calculate_duration

  Command.run!(*cmd) do |line|
    if block_given? && total_duration&.positive?
      progress = parse_progress(line, total_duration)
      yield progress if progress
    end
  end

  yield 100.0 if block_given?

  Media.new(output_path)
end