Skip to content

Commit

Permalink
Animated webp support (#39)
Browse files Browse the repository at this point in the history
* add animated_webp_support decorator

* fix mypy

* revise

* pre-commit

* revise

* add test case

* fix format

* update snapshot

* update snapshot

* update snapshot

* fix snapshot

* support uri as http url

* update snapshot

* manual fix snapshot

* fix snapshot

* fix

* check webpmux installed before call webpmux cmd

* format

* rm webpmux check

* refactor

* format

* update snapshot
  • Loading branch information
Touyu-Pan authored Nov 14, 2023
1 parent af049be commit 365e21b
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/ffmpeg_media_type/tests/__snapshots__/6.0/test_info.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@
'width': 4716,
})
# ---
# name: test_detect[lionic_vp8x_1109.webp][lionic_vp8x_1109.webp]
dict({
'duration': 0.0,
'format': 'webp_pipe',
'height': 520,
'size': 344360,
'suggest_ext': 'webp',
'type': 'image',
'width': 1218,
})
# ---
# name: test_detect[maxresdefault.jpg-sdafdeagwdsadf][maxresdefault.jpg-sdafdeagwdsadf]
dict({
'duration': 0.0,
Expand Down
29 changes: 28 additions & 1 deletion src/ffmpeg_media_type/utils/ffmpeg.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import os
import re
from functools import lru_cache
import tempfile
from collections.abc import Callable
from functools import lru_cache, wraps
from pathlib import Path
from typing import Any, Literal

import requests
from pydantic import BaseModel, Field

from .shell import call
Expand Down Expand Up @@ -87,6 +90,7 @@ def _parse_muxer_info(content: str) -> dict[str, Any]:


def _get_muxer_info(version: str, flag: str, codec: str, description: str) -> FFMpegSupport:

muxer_info = {
"common_exts": [],
"mime_type": "",
Expand Down Expand Up @@ -239,7 +243,30 @@ def get_ffmpeg_version(mode: Literal["major", "minor", "patch"] = "patch") -> st
raise RuntimeError(f"FFmpeg version not found {result}") from e


def animated_webp_support(func: Callable[[str], FFProbeInfo]) -> Callable[[str], FFProbeInfo]:
@wraps(func)
def wrapper(uri: str) -> FFProbeInfo:
probe_info = func(uri)
if probe_info.streams[0].height == 0 and probe_info.streams[0].width == 0 and probe_info.format.format_name == "webp_pipe":
if uri.startswith("http"):
response = requests.get(uri)
response.raise_for_status()
with tempfile.NamedTemporaryFile(mode="wb", suffix=".webp", delete=False) as f:
f.write(response.content)
uri = f.name

assert os.path.exists(uri)
webpmux_command = ["webpmux", "-get", "frame", "1", uri, "-o", uri]
call(webpmux_command)
return func(uri)
return probe_info

return wrapper


@animated_webp_support
def ffprobe(input_url: str) -> FFProbeInfo:

# Construct the FFprobe command with JSON output format
ffprobe_cmd = get_ffprobe() + [
"-v",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,60 @@
]),
})
# ---
# name: test_ffprobe_file[lionic_vp8x_1109.webp][lionic_vp8x_1109.webp]
dict({
'format': dict({
'duration': None,
'format_long_name': None,
'format_name': 'webp_pipe',
'nb_programs': 0,
'nb_streams': 1,
'probe_score': 99,
'size': 344360,
'start_time': None,
}),
'streams': list([
dict({
'avg_frame_rate': '0/0',
'codec_long_name': 'unknown',
'codec_name': 'webp',
'codec_tag': '0x0000',
'codec_tag_string': '[0][0][0][0]',
'codec_time_base': '0/1',
'codec_type': 'video',
'coded_height': 520,
'coded_width': 1218,
'disposition': dict({
'attached_pic': 0,
'clean_effects': 0,
'comment': 0,
'default': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'original': 0,
'timed_thumbnails': 0,
'visual_impaired': 0,
}),
'has_b_frames': 0,
'height': 520,
'index': 0,
'level': -99,
'pix_fmt': 'argb',
'profile': None,
'r_frame_rate': '25/1',
'refs': 1,
'tags': dict({
'rotate': 0,
}),
'time_base': '1/25',
'width': 1218,
}),
]),
})
# ---
# name: test_ffprobe_file[Cle9pQ5.jpg][Cle9pQ5.jpg]
dict({
'format': dict({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,66 @@
]),
})
# ---
# name: test_ffprobe_file[lionic_vp8x_1109.webp][lionic_vp8x_1109.webp]
dict({
'format': dict({
'duration': None,
'format_long_name': None,
'format_name': 'webp_pipe',
'nb_programs': 0,
'nb_streams': 1,
'probe_score': 99,
'size': 344360,
'start_time': None,
}),
'streams': list([
dict({
'avg_frame_rate': '25/1',
'closed_captions': 0,
'codec_long_name': 'unknown',
'codec_name': 'webp',
'codec_tag': '0x0000',
'codec_tag_string': '[0][0][0][0]',
'codec_type': 'video',
'coded_height': 520,
'coded_width': 1218,
'disposition': dict({
'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 0,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0,
}),
'film_grain': 0,
'has_b_frames': 0,
'height': 520,
'index': 0,
'level': -99,
'pix_fmt': 'argb',
'profile': None,
'r_frame_rate': '25/1',
'refs': 1,
'tags': dict({
'rotate': 0,
}),
'time_base': '1/25',
'width': 1218,
}),
]),
})
# ---
# name: test_ffprobe_file[Cle9pQ5.jpg][Cle9pQ5.jpg]
dict({
'format': dict({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,66 @@
]),
})
# ---
# name: test_ffprobe_file[lionic_vp8x_1109.webp][lionic_vp8x_1109.webp]
dict({
'format': dict({
'duration': None,
'format_long_name': None,
'format_name': 'webp_pipe',
'nb_programs': 0,
'nb_streams': 1,
'probe_score': 99,
'size': 344360,
'start_time': None,
}),
'streams': list([
dict({
'avg_frame_rate': '25/1',
'closed_captions': 0,
'codec_long_name': 'unknown',
'codec_name': 'webp',
'codec_tag': '0x0000',
'codec_tag_string': '[0][0][0][0]',
'codec_type': 'video',
'coded_height': 520,
'coded_width': 1218,
'disposition': dict({
'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 0,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0,
}),
'film_grain': 0,
'has_b_frames': 0,
'height': 520,
'index': 0,
'level': -99,
'pix_fmt': 'argb',
'profile': None,
'r_frame_rate': '25/1',
'refs': 1,
'tags': dict({
'rotate': 0,
}),
'time_base': '1/25',
'width': 1218,
}),
]),
})
# ---
# name: test_ffprobe_file[maxresdefault.jpg-sdafdeagwdsadf][maxresdefault.jpg-sdafdeagwdsadf]
dict({
'format': dict({
Expand Down
Binary file not shown.

0 comments on commit 365e21b

Please sign in to comment.