media file: get duration of animated GIFs, PNGs, and ugoiras.
Add methods to MediaFile to calculate the duration, frame count, and frame rate of animated GIFs, PNGs, Ugoiras, and videos. Some considerations: * It's possible to have a GIF or PNG that's technically animated but just has one frame. These are treated as non-animated images. * It's possible to have an animated GIF that has an unspecified frame rate. In this case we assume the frame rate is 10 FPS; this is browser dependent and may not be correct. * Animated GIFs, PNGs, and Ugoiras all support variable frame rates. Technically, each frame has a separate delay, and the delays can be different frame-to-frame. We report only the average frame rate. * Getting the duration of an APNG is surprisingly hard. Most tools don't have good support for APNGs since it's a rare and non-standardized format. The best we can do is get the frame count using ExifTool and the frame rate using ffprobe, then calculate the duration from that.
This commit is contained in:
@@ -40,27 +40,41 @@ class FFmpeg
|
||||
end
|
||||
|
||||
def width
|
||||
video_channels.first[:width]
|
||||
video_streams.first[:width]
|
||||
end
|
||||
|
||||
def height
|
||||
video_channels.first[:height]
|
||||
video_streams.first[:height]
|
||||
end
|
||||
|
||||
def duration
|
||||
metadata.dig(:format, :duration).to_f
|
||||
end
|
||||
|
||||
def video_channels
|
||||
def frame_count
|
||||
if video_streams.first.has_key?(:nb_frames)
|
||||
video_streams.first[:nb_frames].to_i
|
||||
else
|
||||
(duration * frame_rate).to_i
|
||||
end
|
||||
end
|
||||
|
||||
def frame_rate
|
||||
rate = video_streams.first[:avg_frame_rate] # "100/57"
|
||||
numerator, denominator = rate.split("/")
|
||||
(numerator.to_f / denominator.to_f)
|
||||
end
|
||||
|
||||
def video_streams
|
||||
metadata[:streams].select { |stream| stream[:codec_type] == "video" }
|
||||
end
|
||||
|
||||
def audio_channels
|
||||
def audio_streams
|
||||
metadata[:streams].select { |stream| stream[:codec_type] == "audio" }
|
||||
end
|
||||
|
||||
def has_audio?
|
||||
audio_channels.present?
|
||||
audio_streams.present?
|
||||
end
|
||||
|
||||
def shell!(command)
|
||||
|
||||
Reference in New Issue
Block a user