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
This commit is contained in:
evazion
2022-10-27 00:44:36 -05:00
parent 48ecb80d6b
commit a9d586e93a
11 changed files with 61 additions and 28 deletions

View File

@@ -46,11 +46,11 @@ class FFmpeg
end
def width
video_streams.first[:width]
video_stream[:width]
end
def height
video_streams.first[:height]
video_stream[:height]
end
# @see https://trac.ffmpeg.org/wiki/FFprobeTips#Duration
@@ -68,8 +68,8 @@ class FFmpeg
# @return [Integer, nil] The number of frames in the video or animation, or nil if unknown.
def frame_count
if video_streams.first&.has_key?(:nb_frames)
video_streams.first[:nb_frames].to_i
if video_stream.has_key?(:nb_frames)
video_stream[:nb_frames].to_i
elsif playback_info.has_key?(:frame)
playback_info[:frame].to_i
else
@@ -83,6 +83,14 @@ class FFmpeg
frame_count / duration
end
def video_codec
video_stream[:codec_name]
end
def video_stream
video_streams.first || {}
end
def video_streams
metadata[:streams].to_a.select { |stream| stream[:codec_type] == "video" }
end

View File

@@ -156,6 +156,16 @@ class MediaFile
file_ext.in?([:webm, :mp4])
end
# @return [Boolean] True if the file is a MP4.
def is_mp4?
file_ext == :mp4
end
# @return [Boolean] True if the file is a WebM.
def is_webm?
file_ext == :webm
end
# @return [Boolean] true if the file is a Pixiv ugoira
def is_ugoira?
file_ext == :zip

View File

@@ -5,7 +5,7 @@
#
# @see https://github.com/streamio/streamio-ffmpeg
class MediaFile::Video < MediaFile
delegate :duration, :frame_count, :frame_rate, :has_audio?, to: :video
delegate :duration, :frame_count, :frame_rate, :has_audio?, :video_codec, :video_stream, :video_streams, :audio_streams, to: :video
def dimensions
[video.width, video.height]
@@ -16,14 +16,12 @@ class MediaFile::Video < MediaFile
end
def is_supported?
case file_ext
when :webm
metadata["Matroska:DocType"] == "webm"
when :mp4
true
else
false
end
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.