Merge pull request #411 from saimn/video-thumbnail

Fix video thumbnail creation when delay > video length
This commit is contained in:
Simon Conseil
2020-11-26 16:31:55 -03:00
committed by GitHub
5 changed files with 25 additions and 4 deletions

View File

@@ -28,6 +28,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install FFmpeg
run: |
sudo apt update
sudo apt install ffmpeg
ffmpeg -version
- name: Install Tox

View File

@@ -11,6 +11,7 @@ Released on 2020-xx-xx.
- Catch warnings when reading EXIF data.
- Avoid crash when thumbnail cannot be generated [:issue:`401`].
- Replace deprecated usage of `imp`.
- Fix video thumbnail creation when delay > video length [:issue:`411`].
Version 2.1.1
~~~~~~~~~~~~~

View File

@@ -136,10 +136,21 @@ def generate_thumbnail(source, outname, box, delay, fit=True, options=None,
# dump an image of the video
cmd = [converter, '-i', source, '-an', '-r', '1',
'-ss', delay, '-vframes', '1', '-y', tmpfile]
'-ss', str(delay), '-vframes', '1', '-y', tmpfile]
logger.debug('Create thumbnail for video: %s', ' '.join(cmd))
check_subprocess(cmd, source, outname)
# Sometimes ffmpeg fails with returncode zero but without producing an
# output file Thus, we need to check if an output file was created. If
# not, assume ffmpeg failed
if not os.path.isfile(tmpfile):
logger.debug('Thumbnail generation failed. Likely due to very short '
'video length.')
cmd = [converter, '-i', source, '-an', '-r', '1',
'-ss', '0', '-vframes', '1', '-y', tmpfile]
logger.debug('Retry to create thumbnail for video: %s', ' '.join(cmd))
check_subprocess(cmd, source, outname)
# use the generate_thumbnail function from sigal.image
image.generate_thumbnail(tmpfile, outname, box, fit=fit, options=options)
# remove the image

View File

@@ -3,6 +3,7 @@ title = 'Sigal test gallery ☺'
source = 'pictures'
thumb_suffix = '.tn'
keep_orig = True
thumb_video_delay = 5
links = [('Example link', 'http://example.org'),
('Another link', 'http://example.org')]

View File

@@ -3,7 +3,8 @@ import os
import pytest
from sigal.settings import Status, create_settings
from sigal.video import generate_video, process_video, video_size
from sigal.video import (generate_thumbnail, generate_video, process_video,
video_size)
CURRENT_DIR = os.path.dirname(__file__)
TEST_VIDEO = 'example video.ogv'
@@ -17,6 +18,12 @@ def test_video_size():
assert size_src == (0, 0)
def test_generate_thumbnail(tmpdir):
outname = str(tmpdir.join('test.jpg'))
generate_thumbnail(SRCFILE, outname, (50, 50), 5)
assert os.path.isfile(outname)
def test_process_video(tmpdir):
base, ext = os.path.splitext(TEST_VIDEO)
@@ -48,7 +55,7 @@ def test_generate_video_fit_height(tmpdir, fmt):
assert size_dst[0] == 80
# less than 2% error on ratio
assert abs(size_dst[0]/size_dst[1] - size_src[0]/size_src[1]) < 2e-2
assert abs(size_dst[0] / size_dst[1] - size_src[0] / size_src[1]) < 2e-2
@pytest.mark.parametrize("fmt", ['webm', 'mp4'])
@@ -66,7 +73,7 @@ def test_generate_video_fit_width(tmpdir, fmt):
assert size_dst[1] == 50
# less than 2% error on ratio
assert abs(size_dst[0]/size_dst[1] - size_src[0]/size_src[1]) < 2e-2
assert abs(size_dst[0] / size_dst[1] - size_src[0] / size_src[1]) < 2e-2
@pytest.mark.parametrize("fmt", ['webm', 'mp4', 'ogv'])