Class: FFMPEG::Stream
- Inherits:
-
Object
- Object
- FFMPEG::Stream
- Defined in:
- lib/ffmpeg/stream.rb
Overview
Represents a media stream (video, audio, subtitle, etc.)
Constant Summary collapse
- VIDEO =
Stream types
"video"- AUDIO =
"audio"- SUBTITLE =
"subtitle"- DATA =
"data"- ATTACHMENT =
"attachment"
Instance Attribute Summary collapse
-
#avg_frame_rate ⇒ Float?
readonly
Average frame rate (video only).
-
#bit_rate ⇒ Integer?
readonly
Bit rate in bits/sec.
-
#channel_layout ⇒ String?
readonly
Channel layout (stereo, 5.1, etc.).
-
#channels ⇒ Integer?
readonly
Number of audio channels.
-
#codec ⇒ String
readonly
Codec name (h264, aac, etc.).
-
#codec_long_name ⇒ String?
readonly
Codec long name.
-
#default ⇒ Boolean
readonly
Whether this is the default stream.
-
#duration ⇒ Float?
readonly
Duration in seconds.
-
#frame_rate ⇒ Float?
readonly
Frame rate (video only).
-
#height ⇒ Integer?
readonly
Height (video only).
-
#index ⇒ Integer
readonly
Stream index.
-
#language ⇒ String?
readonly
Language code (eng, spa, etc.).
-
#pixel_format ⇒ String?
readonly
Pixel format (yuv420p, etc.).
-
#profile ⇒ String?
readonly
Profile (High, Main, etc.).
-
#raw ⇒ Hash
readonly
Raw stream data from ffprobe.
-
#rotation ⇒ Integer?
readonly
Rotation in degrees.
-
#sample_rate ⇒ Integer?
readonly
Sample rate (audio only).
-
#type ⇒ String
readonly
Stream type (video, audio, subtitle, data).
-
#width ⇒ Integer?
readonly
Width (video only).
Instance Method Summary collapse
-
#aspect_ratio ⇒ Float?
Aspect ratio.
-
#audio? ⇒ Boolean
True if this is an audio stream.
-
#data? ⇒ Boolean
True if this is a data stream.
-
#hd? ⇒ Boolean
True if video is HD (720p or higher).
-
#initialize(data) ⇒ Stream
constructor
Create a new Stream from ffprobe data.
-
#landscape? ⇒ Boolean
True if video is landscape orientation.
-
#portrait? ⇒ Boolean
True if video is portrait orientation.
-
#resolution ⇒ String?
Resolution as “WIDTHxHEIGHT”.
-
#subtitle? ⇒ Boolean
True if this is a subtitle stream.
-
#to_s ⇒ String
Human-readable description.
-
#uhd? ⇒ Boolean
True if video is 4K (2160p or higher).
-
#video? ⇒ Boolean
True if this is a video stream.
Constructor Details
#initialize(data) ⇒ Stream
Create a new Stream from ffprobe data
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? (data["tags"] || {}) parse_disposition(data["disposition"] || {}) end |
Instance Attribute Details
#avg_frame_rate ⇒ Float? (readonly)
Returns average frame rate (video only).
47 48 49 |
# File 'lib/ffmpeg/stream.rb', line 47 def avg_frame_rate @avg_frame_rate end |
#bit_rate ⇒ Integer? (readonly)
Returns bit rate in bits/sec.
50 51 52 |
# File 'lib/ffmpeg/stream.rb', line 50 def bit_rate @bit_rate end |
#channel_layout ⇒ String? (readonly)
Returns channel layout (stereo, 5.1, etc.).
62 63 64 |
# File 'lib/ffmpeg/stream.rb', line 62 def channel_layout @channel_layout end |
#channels ⇒ Integer? (readonly)
Returns number of audio channels.
59 60 61 |
# File 'lib/ffmpeg/stream.rb', line 59 def channels @channels end |
#codec ⇒ String (readonly)
Returns codec name (h264, aac, etc.).
29 30 31 |
# File 'lib/ffmpeg/stream.rb', line 29 def codec @codec end |
#codec_long_name ⇒ String? (readonly)
Returns codec long name.
32 33 34 |
# File 'lib/ffmpeg/stream.rb', line 32 def codec_long_name @codec_long_name end |
#default ⇒ Boolean (readonly)
Returns whether this is the default stream.
74 75 76 |
# File 'lib/ffmpeg/stream.rb', line 74 def default @default end |
#duration ⇒ Float? (readonly)
Returns duration in seconds.
53 54 55 |
# File 'lib/ffmpeg/stream.rb', line 53 def duration @duration end |
#frame_rate ⇒ Float? (readonly)
Returns frame rate (video only).
44 45 46 |
# File 'lib/ffmpeg/stream.rb', line 44 def frame_rate @frame_rate end |
#height ⇒ Integer? (readonly)
Returns height (video only).
41 42 43 |
# File 'lib/ffmpeg/stream.rb', line 41 def height @height end |
#index ⇒ Integer (readonly)
Returns stream index.
23 24 25 |
# File 'lib/ffmpeg/stream.rb', line 23 def index @index end |
#language ⇒ String? (readonly)
Returns language code (eng, spa, etc.).
71 72 73 |
# File 'lib/ffmpeg/stream.rb', line 71 def language @language end |
#pixel_format ⇒ String? (readonly)
Returns pixel format (yuv420p, etc.).
65 66 67 |
# File 'lib/ffmpeg/stream.rb', line 65 def pixel_format @pixel_format end |
#profile ⇒ String? (readonly)
Returns profile (High, Main, etc.).
35 36 37 |
# File 'lib/ffmpeg/stream.rb', line 35 def profile @profile end |
#raw ⇒ Hash (readonly)
Returns raw stream data from ffprobe.
77 78 79 |
# File 'lib/ffmpeg/stream.rb', line 77 def raw @raw end |
#rotation ⇒ Integer? (readonly)
Returns rotation in degrees.
68 69 70 |
# File 'lib/ffmpeg/stream.rb', line 68 def rotation @rotation end |
#sample_rate ⇒ Integer? (readonly)
Returns sample rate (audio only).
56 57 58 |
# File 'lib/ffmpeg/stream.rb', line 56 def sample_rate @sample_rate end |
#type ⇒ String (readonly)
Returns stream type (video, audio, subtitle, data).
26 27 28 |
# File 'lib/ffmpeg/stream.rb', line 26 def type @type end |
#width ⇒ Integer? (readonly)
Returns width (video only).
38 39 40 |
# File 'lib/ffmpeg/stream.rb', line 38 def width @width end |
Instance Method Details
#aspect_ratio ⇒ Float?
Returns 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.
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.
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).
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.
141 142 143 |
# File 'lib/ffmpeg/stream.rb', line 141 def landscape? video? && !portrait? end |
#portrait? ⇒ Boolean
Returns 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 |
#resolution ⇒ String?
Returns 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.
108 109 110 |
# File 'lib/ffmpeg/stream.rb', line 108 def subtitle? type == SUBTITLE end |
#to_s ⇒ String
Returns 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).
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.
98 99 100 |
# File 'lib/ffmpeg/stream.rb', line 98 def video? type == VIDEO end |