Files
danbooru/app/logical/media_file/video.rb
evazion a9d586e93a Fix #3615: Unsupported video codecs.
Don't allow uploading videos with unsupported video codecs.

The only video codecs we allow for MP4 files are H.264 and VP9. Other
codecs, including H.265 (aka HEVC), MPEG-4 part 2, and AV1, are
disallowed because they're not universally supported by browsers.
Firefox doesn't support H.265 or MPEG-4 part 2, and Safari doesn't
support AV1.

Additionally, don't allow videos with multiple video tracks, multiple
audio tracks, or no video tracks. Multiple video and audio tracks are
disallowed because they're rare and for moderation purposes, we don't
want people hiding content in extra tracks.

These restrictions really only apply to MP4 videos, since WebM files
don't support multiple video or audio tracks and only support a limited
number of codecs (VP8 and VP9 for videos, Vorbis and Opus for audio).

There are currently 22 posts with unsupported video codecs:

* https://danbooru.donmai.us/posts?tags=video+is:mp4+-exif:Track1:CompressorID=avc1+-exif:Track2:CompressorID=avc1+-exif:Track1:CompressorID=vp09+-exif:Track2:CompressorID=vp09 # AVC1 is H.264

There is one post that has multiple audio tracks:

* https://danbooru.donmai.us/posts/2382057
2022-10-27 01:43:33 -05:00

44 lines
1.0 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?, :video_codec, :video_stream, :video_streams, :audio_streams, 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 is_supported?
return false if video_streams.size != 1
return false if audio_streams.size > 1
return false if is_webm? && metadata["Matroska:DocType"] != "webm"
return false if is_mp4? && !video_codec.in?(["h264", "vp9"])
true
end
# True if decoding the video fails.
def is_corrupt?
video.playback_info.blank?
end
private
def video
FFmpeg.new(file)
end
def preview_frame
video.smart_video_preview
end
memoize :video, :preview_frame, :dimensions, :duration, :has_audio?
end