Files
danbooru/app/logical/media_file/video.rb
evazion e005520ad8 media assets: save audio volume levels in media metadata.
For videos with sound, save information about audio volume levels in the
media asset's metadata. These values are stored:

* FFmpeg:AudioPeakLoudness       The peak loudness of the audio track, from 0.0 (silent) to 1.0 (max volume)
* FFmpeg:AudioAverageLoudness    The average loudness of the audio track, from 0.0 (silent) to 1.0 (max volume).
* FFmpeg:AudioLoudnessRange      The difference between the quietest and loudest sounds in the audio track (in decibels).
* FFmpeg:AudioSilencePercentage  The percentage of the video that is silent (1.0 is completely silent, 0.5 is 50% silence, 0.0 is no silence).

These values are calculated based on the EBU R 128 standard, using the ffmpeg command below:

  ffmpeg -i file.mp4 -af silencedetect=duration=0.05:noise=0.0001,ebur128=metadata=1:peak=true:dualmono=true -f null /dev/null

See the links below for details:

* https://en.wikipedia.org/wiki/EBU_R_128
* https://www.ffmpeg.org/ffmpeg-filters.html#ebur128-1
* https://tech.ebu.ch/loudness
* https://tech.ebu.ch/docs/tech/tech3341.pdf
2022-11-04 18:06:30 -05:00

73 lines
2.7 KiB
Ruby

# frozen_string_literal: true
# A MediaFile for a webm or mp4 video. Uses ffmpeg to generate preview
# thumbnails.
#
# @see https://github.com/streamio/streamio-ffmpeg
class MediaFile::Video < MediaFile
delegate :duration, :frame_count, :frame_rate, :has_audio?, :is_corrupt?, :major_brand, :pix_fmt,
:video_codec, :video_bit_rate, :video_stream, :video_streams, :audio_codec, :audio_bit_rate,
:audio_stream, :audio_streams, :silence_duration, :silence_percentage, :average_loudness,
:peak_loudness, :loudness_range, :error, to: :video
def dimensions
[video.width, video.height]
end
def preview!(max_width, max_height, **options)
preview_frame.preview!(max_width, max_height, **options)
end
def metadata
super.merge({
"FFmpeg:Error" => error,
"FFmpeg:MajorBrand" => major_brand,
"FFmpeg:PixFmt" => pix_fmt,
"FFmpeg:FrameCount" => frame_count,
"FFmpeg:VideoCodec" => video_codec,
"FFmpeg:VideoProfile" => video_stream[:profile],
"FFmpeg:VideoBitRate" => video_bit_rate,
"FFmpeg:AudioCodec" => audio_codec,
"FFmpeg:AudioProfile" => audio_stream[:profile],
"FFmpeg:AudioLayout" => audio_stream[:channel_layout],
"FFmpeg:AudioBitRate" => audio_bit_rate,
"FFmpeg:AudioPeakLoudness" => peak_loudness,
"FFmpeg:AudioAverageLoudness" => average_loudness,
"FFmpeg:AudioLoudnessRange" => loudness_range,
"FFmpeg:AudioSilencePercentage" => silence_percentage,
}.compact_blank)
end
def is_supported?
return false if video_streams.size != 1
return false if audio_streams.size > 1
return false if is_webm? && exif_metadata["Matroska:DocType"] != "webm"
return false if is_mp4? && !video_codec.in?(["h264", "vp9"])
# Only allow pixel formats supported by most browsers. Don't allow 10-bit video or 4:4:4 subsampling (neither are supported by Firefox).
#
# yuv420p: 8-bit YUV, 4:2:0 subsampling. The vast majority of videos use this format.
# yuvj420p: 8-bit YUV, 4:2:0 subsampling, color range restricted to 16-235. Uncommon, but widely supported.
# yuv444p: 8-bit YUV, 4:4:4 subsampling (i.e. no subsampling). Uncommon, not supported by Firefox.
# yuv420p10le: 10-bit YUV, 4:2:0 subsampling (i.e. 10-bit video). Uncommon, not supported by Firefox.
# gbrp: 8-bit RGB (used by VP9). Uncommon, but widely supported.
#
# https://github.com/FFmpeg/FFmpeg/blob/master/libavutil/pixfmt.h
return false if !pix_fmt.in?(%w[yuv420p yuvj420p gbrp])
true
end
private
def video
FFmpeg.new(self)
end
def preview_frame
video.smart_video_preview
end
memoize :video, :preview_frame, :dimensions, :metadata, :duration, :has_audio?
end