Skip to content

Commit

Permalink
Add video thumbnail extraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Mar 20, 2022
1 parent 41cde7f commit d8343f6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
34 changes: 34 additions & 0 deletions iscc_sdk/video.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""*Video handling module*"""
import io
import subprocess
import sys
import tempfile
from os.path import join, basename
from PIL import Image, ImageEnhance
import iscc_sdk as idk
import iscc_schema as iss


__all__ = [
"video_meta_extract",
"video_meta_embed",
"video_thumbnail",
]

VIDEO_META_MAP = {
Expand Down Expand Up @@ -135,3 +138,34 @@ def video_meta_embed(fp, meta):
]
subprocess.run(cmd, capture_output=True, check=True)
return videofile


def video_thumbnail(fp):
# type: (str) -> Image.Image
"""
Create a thumbnail for a video.
:param str fp: Filepath to video file.
:return: Raw PNG byte data
:rtype: bytes
"""
size = idk.sdk_opts.image_thumbnail_size

cmd = [
idk.ffmpeg_bin(),
"-i",
fp,
"-vf",
f"thumbnail,scale={size}:-1",
"-frames:v",
"1",
"-c:v",
"png",
"-f",
"image2pipe",
"-",
]

result = subprocess.run(cmd, capture_output=True)
img_obj = Image.open(io.BytesIO(result.stdout))
return ImageEnhance.Sharpness(img_obj.convert("RGB")).enhance(1.4)
7 changes: 7 additions & 0 deletions tests/test_video.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import os

from PIL import Image

import iscc_sdk as idk
import iscc_schema as iss

Expand Down Expand Up @@ -62,3 +64,8 @@ def test_video_metadata_escaping(mp4_file):
description="Multi\nLine\n\nDescription with ; and other = crazy characters\n",
)
os.remove(new_file)


def test_video_thumbnail(mp4_file):
thumb = idk.video_thumbnail(mp4_file)
assert isinstance(thumb, Image.Image)

0 comments on commit d8343f6

Please sign in to comment.