Skip to content

Commit

Permalink
fix #30, support test in different ffmpeg version (#31)
Browse files Browse the repository at this point in the history
* fix #30, support test in different ffmpeg version

* add conftest

* only use minor version

* add snapshot

* use get_ffmpeg_version with mode

* add tox.ini

* revert

* check mock is called

* use relative path

* add duration in test case

* fix test

* fix test

* fix test

* fix test

* fix

* add normal test

* sudo

* fix #23, handle mjpeg (#33)

* fix #23, handle mjpeg

* fix mjpeg

* fix test_get_ffmpeg_version
  • Loading branch information
lucemia authored Jul 21, 2023
1 parent a06aecf commit 1cee6c1
Show file tree
Hide file tree
Showing 34 changed files with 14,332 additions and 41 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ jobs:
- name: Lint with pre-commit
run: |
poetry run pre-commit run --all-files
- name: Test with pytest
- name: Test with ffmpeg 4.0
run: |
sudo apt install ffmpeg
poetry run pytest ./src --cov=./src -s
env: # Or as an environment variable
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
poetry run ./scripts/test.sh 4.0
- name: Test with ffmpeg 5.0
run: |
poetry run ./scripts/test.sh 5.0
- name: Test with ffmpeg 6.0
run: |
poetry run ./scripts/test.sh 6.0
- name: Test without docker
run: |
sudo apt-get update
sudo apt-get install ffmpeg
poetry run py.test ./src --cov=./src -s -m "not ffmpeg_version" --cov-append
- name: Coveralls
uses: coverallsapp/github-action@v2
Empty file modified scripts/build.sh
100644 → 100755
Empty file.
11 changes: 11 additions & 0 deletions scripts/test-update-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
# basic reference for writing script for travis

# Define the array of versions
versions=(3.2 3.3 3.4 4.0 4.1 4.2 4.3 4.4 5.0 5.1 6.0)

# Loop through the array and execute the test.sh script for each version
for version in "${versions[@]}"
do
./scripts/test.sh "$version" --snapshot-update
done
4 changes: 4 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
# basic reference for writing script for travis

FFMPEG_DOCKER_VERSION=${1:-6.0} poetry run pytest ./src --cov=./src -s -m "ffmpeg_version" "$2" --cov-append
25 changes: 25 additions & 0 deletions src/ffmpeg_media_type/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path

import pytest
from syrupy.assertion import SnapshotAssertion
from syrupy.extensions.amber import AmberSnapshotExtension
from syrupy.location import PyTestLocation

from .utils.ffmpeg import get_ffmpeg_version


@pytest.fixture(scope="session")
def ffmpeg_version() -> str:
return get_ffmpeg_version("minor")


class DifferentDirectoryExtension(AmberSnapshotExtension):
@classmethod
def dirname(cls, *, test_location: "PyTestLocation") -> str:
version = get_ffmpeg_version("minor")
return str(Path(test_location.filepath).parent / "__snapshots__" / version)


@pytest.fixture
def snapshot_ffmpeg(snapshot: SnapshotAssertion) -> SnapshotAssertion:
return snapshot.use_extension(DifferentDirectoryExtension)
16 changes: 6 additions & 10 deletions src/ffmpeg_media_type/info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import subprocess
from typing import Literal
from urllib.parse import urlparse

from pydantic import BaseModel

from .utils.ffmpeg import FFProbeInfo, ffprobe, load_cache
from .utils.ffmpeg import FFProbeInfo, ffprobe, get_ffmpeg, load_cache
from .utils.shell import call


class MediaInfo(BaseModel):
Expand All @@ -21,8 +21,7 @@ class MediaInfo(BaseModel):


def generate_thumbnail(video_path: str, thumbnail_path: str, time_offset: float = 0) -> str:
ffmpeg_cmd = [
"ffmpeg", # FFmpeg command
ffmpeg_cmd = get_ffmpeg() + [
"-y", # Overwrite output file if it exists
"-i",
video_path, # Input video path
Expand All @@ -37,11 +36,7 @@ def generate_thumbnail(video_path: str, thumbnail_path: str, time_offset: float
thumbnail_path, # Output thumbnail path
]

try:
subprocess.check_output(ffmpeg_cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print("Error generating thumbnail:", e.output)
raise
call(ffmpeg_cmd)

return thumbnail_path

Expand All @@ -62,6 +57,7 @@ def _extract_file_extension(uri: str) -> str:
"gif_pipe": ["gif"],
"jpeg_pipe": ["jpeg", "jpg"],
"webp_pipe": ["webp"],
"mjpeg": ["jpg", "jpeg", "mjpeg"],
}


Expand Down Expand Up @@ -95,7 +91,7 @@ def _guess_media_info(
suggest_ext = None

# NOTE: we classify gif as image
if not duration or format_name == "gif":
if not duration or format_name in ("gif", "mjpeg"):
return MediaInfo(
type="image",
width=info.streams[0].width or 0,
Expand Down
144 changes: 144 additions & 0 deletions src/ffmpeg_media_type/tests/__snapshots__/3.2/test_info.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# serializer version: 1
# name: test_detect[10257640.mp4][10257640.mp4]
dict({
'duration': 6.803333,
'format': 'mov,mp4,m4a,3gp,3g2,mj2',
'height': 480,
'size': 593915,
'suggest_ext': 'mp4',
'type': 'video',
'width': 640,
})
# ---
# name: test_detect[188794898-eeb81e4b-b33a-4bdf-85a8-3b4b8460b88a.png][188794898-eeb81e4b-b33a-4bdf-85a8-3b4b8460b88a.png]
dict({
'duration': 0.0,
'format': 'png_pipe',
'height': 259,
'size': 47210,
'suggest_ext': 'png',
'type': 'image',
'width': 262,
})
# ---
# name: test_detect[201432913186_fish.gif][201432913186_fish.gif]
dict({
'duration': 0.0,
'format': 'gif',
'height': 360,
'size': 30333,
'suggest_ext': 'gif',
'type': 'image',
'width': 480,
})
# ---
# name: test_detect[4ox1IBJxK3cVd0uJA6gOtceSP3g.jpg][4ox1IBJxK3cVd0uJA6gOtceSP3g.jpg]
dict({
'duration': 0.04,
'format': 'mjpeg',
'height': 836,
'size': 1249892,
'suggest_ext': 'jpg',
'type': 'image',
'width': 1254,
})
# ---
# name: test_detect[9b5174019e8c9bec190903528ff6d0392a2d6a9b.mp4][9b5174019e8c9bec190903528ff6d0392a2d6a9b.mp4]
dict({
'duration': 38.741,
'format': 'mov,mp4,m4a,3gp,3g2,mj2',
'height': 1080,
'size': 7164941,
'suggest_ext': 'mp4',
'type': 'video',
'width': 1920,
})
# ---
# name: test_detect[9c7ee468-24a3-4930-9832-73f0f6001ab1.webp][9c7ee468-24a3-4930-9832-73f0f6001ab1.webp]
dict({
'duration': 0.0,
'format': 'webp_pipe',
'height': 787,
'size': 55242,
'suggest_ext': 'webp',
'type': 'image',
'width': 1920,
})
# ---
# name: test_detect[Cle9pQ5.jpg][Cle9pQ5.jpg]
dict({
'duration': 0.04,
'format': 'mjpeg',
'height': 668,
'size': 72973,
'suggest_ext': 'jpg',
'type': 'image',
'width': 720,
})
# ---
# name: test_detect[SampleAudio_0.4mb.mp3][SampleAudio_0.4mb.mp3]
dict({
'duration': 27.74475,
'format': 'mp3',
'height': 0,
'size': 443926,
'suggest_ext': 'mp3',
'type': 'audio',
'width': 0,
})
# ---
# name: test_detect[SampleVideo_1280x720_1mb.mp4][SampleVideo_1280x720_1mb.mp4]
dict({
'duration': 5.312,
'format': 'mov,mp4,m4a,3gp,3g2,mj2',
'height': 720,
'size': 1055736,
'suggest_ext': 'mp4',
'type': 'video',
'width': 1280,
})
# ---
# name: test_detect[cd89b1bf161582d55161f5ba94d03cc7728825c2.jpe][cd89b1bf161582d55161f5ba94d03cc7728825c2.jpe]
dict({
'duration': 0.0,
'format': 'jpeg_pipe',
'height': 3144,
'size': 12642182,
'suggest_ext': 'jpeg',
'type': 'image',
'width': 4716,
})
# ---
# name: test_detect[maxresdefault.jpg-sdafdeagwdsadf][maxresdefault.jpg-sdafdeagwdsadf]
dict({
'duration': 0.0,
'format': 'jpeg_pipe',
'height': 720,
'size': 99802,
'suggest_ext': 'jpeg',
'type': 'image',
'width': 1280,
})
# ---
# name: test_detect[overlay.png][overlay.png]
dict({
'duration': 0.0,
'format': 'png_pipe',
'height': 117,
'size': 2212,
'suggest_ext': 'png',
'type': 'image',
'width': 163,
})
# ---
# name: test_detect[silent_video.mp4][silent_video.mp4]
dict({
'duration': 6.965,
'format': 'mov,mp4,m4a,3gp,3g2,mj2',
'height': 1080,
'size': 14770522,
'suggest_ext': 'mp4',
'type': 'video',
'width': 1920,
})
# ---
Loading

0 comments on commit 1cee6c1

Please sign in to comment.