1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use FFmpeg scene detection for video previews

Generating a video preview by capturing only the first frame of a video
is problematic for videos that begin with a fade in from black.  By
using keyframe and scene detection that is built in to FFmpeg, we can
generate a more representative preview.
This commit is contained in:
Jonathan Hefner 2020-11-17 16:25:01 -06:00 committed by GitHub
parent 8e9d550aee
commit 3397924e66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -28,7 +28,21 @@ module ActiveStorage
private
def draw_relevant_frame_from(file, &block)
draw self.class.ffmpeg_path, "-i", file.path, "-y", "-vframes", "1", "-f", "image2", "-", &block
ffmpeg_args = [
"-i", file.path,
"-vf",
# Select the first video frame, plus keyframes and frames
# that meet the scene change threshold.
'select=eq(n\,0)+eq(key\,1)+gt(scene\,0.015),' +
# Loop the first 1-2 selected frames in case we were only
# able to select 1 frame, then drop the first looped frame.
# This lets us use the first video frame as a fallback.
"loop=loop=-1:size=2,trim=start_frame=1",
"-frames:v", "1",
"-f", "image2", "-",
]
draw self.class.ffmpeg_path, *ffmpeg_args, &block
end
end
end

View file

@ -559,7 +559,7 @@ the box, Active Storage supports previewing videos and PDF documents.
</ul>
```
WARNING: Extracting previews requires third-party applications, FFmpeg for
WARNING: Extracting previews requires third-party applications, FFmpeg v3.4+ for
video and muPDF for PDFs, and on macOS also XQuartz and Poppler.
These libraries are not provided by Rails. You must install them yourself to
use the built-in previewers. Before you install and use third-party software,