From 94ce7d52ccc65504ec4b4dadc5cc4134b16cde53 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 25 Nov 2020 23:20:41 -0300 Subject: [PATCH 1/4] Fix video thumbnail creation when delay > video length --- sigal/video.py | 13 ++++++++++++- tests/sample/sigal.conf.py | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sigal/video.py b/sigal/video.py index 4a2a9d1..a595e8a 100644 --- a/sigal/video.py +++ b/sigal/video.py @@ -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 diff --git a/tests/sample/sigal.conf.py b/tests/sample/sigal.conf.py index 4db648a..50ead1c 100644 --- a/tests/sample/sigal.conf.py +++ b/tests/sample/sigal.conf.py @@ -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')] From 8005a8a6d3000d21d01dbc17efe685ba83191510 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 25 Nov 2020 23:33:21 -0300 Subject: [PATCH 2/4] Add test --- tests/test_video.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_video.py b/tests/test_video.py index e35bf3b..4b17b6c 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -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']) From bf66548a9f246842671d8bb171f53ff4ac4a557b Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 25 Nov 2020 23:44:38 -0300 Subject: [PATCH 3/4] Update changelog --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 69824df..5beca94 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 ~~~~~~~~~~~~~ From 0edb12d7a86dc8fb08db3221208dd8f8c1e61efd Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Thu, 26 Nov 2020 16:14:27 -0300 Subject: [PATCH 4/4] Update apt before installing ffmpeg --- .github/workflows/python-tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index d8defd5..b9b01f3 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -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