Class: FFmpegCore::Movie

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ffmpeg_core/movie.rb

Overview

Modern API for working with video files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Movie

Returns a new instance of Movie.



10
11
12
13
# File 'lib/ffmpeg_core/movie.rb', line 10

def initialize(path)
  @path = path
  @probe = Probe.new(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/ffmpeg_core/movie.rb', line 8

def path
  @path
end

#probeObject (readonly)

Returns the value of attribute probe.



8
9
10
# File 'lib/ffmpeg_core/movie.rb', line 8

def probe
  @probe
end

Instance Method Details

#cut(output_path, options = {}) ⇒ String

Cut/trim a segment from video

Parameters:

  • output_path (String)

    Path to output file

  • options (Hash) (defaults to: {})

    Cut options

Options Hash (options):

  • :start_time (Integer, Float)

    Start time in seconds

  • :duration (Integer, Float)

    Duration in seconds

  • :end_time (Integer, Float)

    End time in seconds (alternative to :duration)

Returns:

  • (String)

    Path to output file



64
65
66
67
# File 'lib/ffmpeg_core/movie.rb', line 64

def cut(output_path, options = {})
  clipper = Clipper.new(path, output_path, options)
  clipper.run
end

#extract_audio(output_path, options = {}) ⇒ String

Extract audio track from video

Parameters:

  • output_path (String)

    Path to output audio file

  • options (Hash) (defaults to: {})

    Extraction options

Options Hash (options):

  • :codec (String)

    Audio codec (e.g., “libmp3lame”, “aac”)

Returns:

  • (String)

    Path to output file



75
76
77
78
# File 'lib/ffmpeg_core/movie.rb', line 75

def extract_audio(output_path, options = {})
  extractor = AudioExtractor.new(path, output_path, options)
  extractor.run
end

#screenshot(output_path, options = {}) ⇒ String

Extract screenshot from video

Parameters:

  • output_path (String)

    Path to output image

  • options (Hash) (defaults to: {})

    Screenshot options

Options Hash (options):

  • :seek_time (Integer, Float)

    Time in seconds to seek to (default: 0)

  • :resolution (String)

    Resolution (e.g., “640x360”)

  • :quality (Integer)

    JPEG quality 2-31, lower is better (default: 2)

Returns:

  • (String)

    Path to screenshot file



51
52
53
54
# File 'lib/ffmpeg_core/movie.rb', line 51

def screenshot(output_path, options = {})
  screenshotter = Screenshot.new(path, output_path, options)
  screenshotter.extract
end

#screenshots(output_dir, count: 5) ⇒ Array<String>

Extract multiple screenshots at equal intervals

Parameters:

  • output_dir (String)

    Directory to save screenshots

  • count (Integer) (defaults to: 5)

    Number of screenshots to extract (default: 5)

Returns:

  • (Array<String>)

    Paths to screenshot files



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ffmpeg_core/movie.rb', line 85

def screenshots(output_dir, count: 5)
  FileUtils.mkdir_p(output_dir)
  total = duration || 0
  interval = total / (count + 1).to_f

  (1..count).map do |i|
    seek = (interval * i).round(2)
    output_path = File.join(output_dir, format("screenshot_%03d.jpg", i))
    Screenshot.new(path, output_path, seek_time: seek).extract
    output_path
  end
end

#transcode(output_path, options = {}) {|Float| ... } ⇒ String

Transcode video with modern API

Parameters:

  • output_path (String)

    Path to output file

  • options (Hash) (defaults to: {})

    Transcoding options

Options Hash (options):

  • :video_codec (String)

    Video codec (e.g., “libx264”)

  • :audio_codec (String)

    Audio codec (e.g., “aac”)

  • :video_bitrate (String, Integer)

    Video bitrate (e.g., “1000k” or 1000)

  • :audio_bitrate (String, Integer)

    Audio bitrate (e.g., “128k” or 128)

  • :resolution (String)

    Resolution (e.g., “1280x720”)

  • :frame_rate (Integer, Float)

    Frame rate (e.g., 30)

  • :filter_graph (Array<String>, String)

    Complex filter graph (e.g., [“[0:v]crop=…”])

  • :maps (Array<String>, String)

    Stream maps (e.g., [“[outv]”, “0:a”])

  • :hwaccel (Symbol)

    Hardware acceleration (:nvenc, :vaapi, :qsv)

  • :custom (Array<String>)

    Custom FFmpeg flags

Yields:

  • (Float)

    Progress ratio (0.0 to 1.0)

Returns:

  • (String)

    Path to transcoded file



35
36
37
38
39
40
41
# File 'lib/ffmpeg_core/movie.rb', line 35

def transcode(output_path, options = {}, &block)
  # Inject duration for progress calculation if known
  options[:duration] ||= duration

  transcoder = Transcoder.new(path, output_path, options)
  transcoder.run(&block)
end