Class: FFMPEG::Stream

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

Overview

Represents a media stream (video, audio, subtitle, etc.)

Examples:

stream = media.video_streams.first
stream.codec           # => "h264"
stream.width           # => 1920
stream.height          # => 1080
stream.frame_rate      # => 29.97
stream.video?          # => true

Constant Summary collapse

VIDEO =

Stream types

"video"
AUDIO =
"audio"
SUBTITLE =
"subtitle"
DATA =
"data"
ATTACHMENT =
"attachment"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Stream

Create a new Stream from ffprobe data

Parameters:

  • data (Hash)

    stream data from ffprobe JSON



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ffmpeg/stream.rb', line 81

def initialize(data)
  @raw = data
  @index = data["index"]
  @type = data["codec_type"]
  @codec = data["codec_name"]
  @codec_long_name = data["codec_long_name"]
  @profile = data["profile"]
  @bit_rate = data["bit_rate"]&.to_i
  @duration = data["duration"]&.to_f

  parse_video_attributes(data) if video?
  parse_audio_attributes(data) if audio?
  parse_tags(data["tags"] || {})
  parse_disposition(data["disposition"] || {})
end

Instance Attribute Details

#avg_frame_rateFloat? (readonly)

Returns average frame rate (video only).

Returns:

  • (Float, nil)

    average frame rate (video only)



47
48
49
# File 'lib/ffmpeg/stream.rb', line 47

def avg_frame_rate
  @avg_frame_rate
end

#bit_rateInteger? (readonly)

Returns bit rate in bits/sec.

Returns:

  • (Integer, nil)

    bit rate in bits/sec



50
51
52
# File 'lib/ffmpeg/stream.rb', line 50

def bit_rate
  @bit_rate
end

#channel_layoutString? (readonly)

Returns channel layout (stereo, 5.1, etc.).

Returns:

  • (String, nil)

    channel layout (stereo, 5.1, etc.)



62
63
64
# File 'lib/ffmpeg/stream.rb', line 62

def channel_layout
  @channel_layout
end

#channelsInteger? (readonly)

Returns number of audio channels.

Returns:

  • (Integer, nil)

    number of audio channels



59
60
61
# File 'lib/ffmpeg/stream.rb', line 59

def channels
  @channels
end

#codecString (readonly)

Returns codec name (h264, aac, etc.).

Returns:

  • (String)

    codec name (h264, aac, etc.)



29
30
31
# File 'lib/ffmpeg/stream.rb', line 29

def codec
  @codec
end

#codec_long_nameString? (readonly)

Returns codec long name.

Returns:

  • (String, nil)

    codec long name



32
33
34
# File 'lib/ffmpeg/stream.rb', line 32

def codec_long_name
  @codec_long_name
end

#defaultBoolean (readonly)

Returns whether this is the default stream.

Returns:

  • (Boolean)

    whether this is the default stream



74
75
76
# File 'lib/ffmpeg/stream.rb', line 74

def default
  @default
end

#durationFloat? (readonly)

Returns duration in seconds.

Returns:

  • (Float, nil)

    duration in seconds



53
54
55
# File 'lib/ffmpeg/stream.rb', line 53

def duration
  @duration
end

#frame_rateFloat? (readonly)

Returns frame rate (video only).

Returns:

  • (Float, nil)

    frame rate (video only)



44
45
46
# File 'lib/ffmpeg/stream.rb', line 44

def frame_rate
  @frame_rate
end

#heightInteger? (readonly)

Returns height (video only).

Returns:

  • (Integer, nil)

    height (video only)



41
42
43
# File 'lib/ffmpeg/stream.rb', line 41

def height
  @height
end

#indexInteger (readonly)

Returns stream index.

Returns:

  • (Integer)

    stream index



23
24
25
# File 'lib/ffmpeg/stream.rb', line 23

def index
  @index
end

#languageString? (readonly)

Returns language code (eng, spa, etc.).

Returns:

  • (String, nil)

    language code (eng, spa, etc.)



71
72
73
# File 'lib/ffmpeg/stream.rb', line 71

def language
  @language
end

#pixel_formatString? (readonly)

Returns pixel format (yuv420p, etc.).

Returns:

  • (String, nil)

    pixel format (yuv420p, etc.)



65
66
67
# File 'lib/ffmpeg/stream.rb', line 65

def pixel_format
  @pixel_format
end

#profileString? (readonly)

Returns profile (High, Main, etc.).

Returns:

  • (String, nil)

    profile (High, Main, etc.)



35
36
37
# File 'lib/ffmpeg/stream.rb', line 35

def profile
  @profile
end

#rawHash (readonly)

Returns raw stream data from ffprobe.

Returns:

  • (Hash)

    raw stream data from ffprobe



77
78
79
# File 'lib/ffmpeg/stream.rb', line 77

def raw
  @raw
end

#rotationInteger? (readonly)

Returns rotation in degrees.

Returns:

  • (Integer, nil)

    rotation in degrees



68
69
70
# File 'lib/ffmpeg/stream.rb', line 68

def rotation
  @rotation
end

#sample_rateInteger? (readonly)

Returns sample rate (audio only).

Returns:

  • (Integer, nil)

    sample rate (audio only)



56
57
58
# File 'lib/ffmpeg/stream.rb', line 56

def sample_rate
  @sample_rate
end

#typeString (readonly)

Returns stream type (video, audio, subtitle, data).

Returns:

  • (String)

    stream type (video, audio, subtitle, data)



26
27
28
# File 'lib/ffmpeg/stream.rb', line 26

def type
  @type
end

#widthInteger? (readonly)

Returns width (video only).

Returns:

  • (Integer, nil)

    width (video only)



38
39
40
# File 'lib/ffmpeg/stream.rb', line 38

def width
  @width
end

Instance Method Details

#aspect_ratioFloat?

Returns aspect ratio.

Returns:

  • (Float, nil)

    aspect ratio



125
126
127
128
129
# File 'lib/ffmpeg/stream.rb', line 125

def aspect_ratio
  return nil unless video? && width && height && height.positive?

  width.to_f / height
end

#audio?Boolean

Returns true if this is an audio stream.

Returns:

  • (Boolean)

    true if this is an audio stream



103
104
105
# File 'lib/ffmpeg/stream.rb', line 103

def audio?
  type == AUDIO
end

#data?Boolean

Returns true if this is a data stream.

Returns:

  • (Boolean)

    true if this is a data stream



113
114
115
# File 'lib/ffmpeg/stream.rb', line 113

def data?
  type == DATA
end

#hd?Boolean

Returns true if video is HD (720p or higher).

Returns:

  • (Boolean)

    true if video is HD (720p or higher)



146
147
148
149
150
151
# File 'lib/ffmpeg/stream.rb', line 146

def hd?
  return false unless video?

  min_dimension = [width, height].compact.min
  min_dimension && min_dimension >= 720
end

#landscape?Boolean

Returns true if video is landscape orientation.

Returns:

  • (Boolean)

    true if video is landscape orientation



141
142
143
# File 'lib/ffmpeg/stream.rb', line 141

def landscape?
  video? && !portrait?
end

#portrait?Boolean

Returns true if video is portrait orientation.

Returns:

  • (Boolean)

    true if video is portrait orientation



132
133
134
135
136
137
138
# File 'lib/ffmpeg/stream.rb', line 132

def portrait?
  return false unless video? && aspect_ratio

  # Account for rotation
  effective_ratio = [90, 270].include?(rotation.to_i) ? 1.0 / aspect_ratio : aspect_ratio
  effective_ratio < 1.0
end

#resolutionString?

Returns resolution as “WIDTHxHEIGHT”.

Returns:

  • (String, nil)

    resolution as “WIDTHxHEIGHT”



118
119
120
121
122
# File 'lib/ffmpeg/stream.rb', line 118

def resolution
  return nil unless video? && width && height

  "#{width}x#{height}"
end

#subtitle?Boolean

Returns true if this is a subtitle stream.

Returns:

  • (Boolean)

    true if this is a subtitle stream



108
109
110
# File 'lib/ffmpeg/stream.rb', line 108

def subtitle?
  type == SUBTITLE
end

#to_sString

Returns human-readable description.

Returns:

  • (String)

    human-readable description



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ffmpeg/stream.rb', line 162

def to_s
  parts = ["Stream ##{index}: #{type.capitalize}"]
  parts << codec if codec

  if video?
    parts << resolution if resolution
    parts << "#{frame_rate}fps" if frame_rate
  end

  if audio?
    parts << "#{sample_rate}Hz" if sample_rate
    parts << channel_layout if channel_layout
  end

  parts.join(", ")
end

#uhd?Boolean

Returns true if video is 4K (2160p or higher).

Returns:

  • (Boolean)

    true if video is 4K (2160p or higher)



154
155
156
157
158
159
# File 'lib/ffmpeg/stream.rb', line 154

def uhd?
  return false unless video?

  min_dimension = [width, height].compact.min
  min_dimension && min_dimension >= 2160
end

#video?Boolean

Returns true if this is a video stream.

Returns:

  • (Boolean)

    true if this is a video stream



98
99
100
# File 'lib/ffmpeg/stream.rb', line 98

def video?
  type == VIDEO
end