-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from kevinmcmurtrie/patch-1
Fix VP8 low bitrate
- Loading branch information
Showing
8 changed files
with
186 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
This folder contains some tooling around zimscraperlib: | ||
- `encode_video.py`: a small utility to encode a video with an existing video preset, just like a scraper would do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from zimscraperlib import logger | ||
from zimscraperlib.video import presets, reencode | ||
|
||
|
||
def encode_video(src_path: Path, dst_path: Path, preset: str): | ||
if not src_path.exists(): | ||
raise ValueError(f"{src_path} does not exists") | ||
try: | ||
preset_cls = getattr(presets, preset) | ||
except AttributeError: | ||
logger.error(f"{preset} preset not found") | ||
raise | ||
logger.info(f"Encoding video {src_path} with {preset} version {preset_cls.VERSION}") | ||
success, process = reencode( | ||
src_path=src_path, | ||
dst_path=dst_path, | ||
ffmpeg_args=preset_cls().to_ffmpeg_args(), | ||
with_process=True, | ||
) # pyright: ignore[reportGeneralTypeIssues] (returned type is variable, depending on `with_process` value) | ||
if not success: | ||
logger.error(f"conversion failed:\n{process.stdout}") | ||
|
||
|
||
def run(args: List[str] = sys.argv): | ||
if len(args) < 4: # noqa: PLR2004 | ||
print(f"Usage: {args[0]} <src_path> <dst_path> <preset>") # noqa: T201 | ||
print( # noqa: T201 | ||
"\t<src_path>\tpath to the video to encode." | ||
"\t<dst_path>\tpath to the store the reencoded video." | ||
"\t<preset>\tname of preset to use." | ||
) | ||
return 1 | ||
encode_video(Path(args[1]), Path(args[2]), args[3]) | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(run()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import re | ||
from pathlib import Path | ||
from typing import List, Optional | ||
|
||
import pytest | ||
|
||
from zimscraperlib.video.encoding import _build_ffmpeg_args | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"src_path,tmp_path,ffmpeg_args,threads,expected", | ||
[ | ||
( | ||
Path("path1/file1.mp4"), | ||
Path("path1/fileout.mp4"), | ||
[ | ||
"-codec:v", | ||
"libx265", | ||
], | ||
None, | ||
[ | ||
"/usr/bin/env", | ||
"ffmpeg", | ||
"-y", | ||
"-i", | ||
"file:path1/file1.mp4", | ||
"-codec:v", | ||
"libx265", | ||
"file:path1/fileout.mp4", | ||
], | ||
), | ||
( | ||
Path("path2/file2.mp4"), | ||
Path("path12/tmpfile.mp4"), | ||
[ | ||
"-b:v", | ||
"300k", | ||
], | ||
1, | ||
[ | ||
"/usr/bin/env", | ||
"ffmpeg", | ||
"-y", | ||
"-i", | ||
"file:path2/file2.mp4", | ||
"-b:v", | ||
"300k", | ||
"-threads", | ||
"1", | ||
"file:path12/tmpfile.mp4", | ||
], | ||
), | ||
( | ||
Path("path2/file2.mp4"), | ||
Path("path12/tmpfile.mp4"), | ||
[ | ||
"-b:v", | ||
"300k", | ||
"-threads", | ||
"1", | ||
], | ||
1, | ||
None, | ||
), | ||
], | ||
) | ||
def test_build_ffmpeg_args( | ||
src_path: Path, | ||
tmp_path: Path, | ||
ffmpeg_args: List[str], | ||
threads: Optional[int], | ||
expected: Optional[List[str]], | ||
): | ||
if expected: | ||
assert ( | ||
_build_ffmpeg_args( | ||
src_path=src_path, | ||
tmp_path=tmp_path, | ||
ffmpeg_args=ffmpeg_args, | ||
threads=threads, | ||
) | ||
== expected | ||
) | ||
else: | ||
with pytest.raises( | ||
AttributeError, | ||
match=re.escape("Cannot set the number of threads, already set"), | ||
): | ||
_build_ffmpeg_args( | ||
src_path=src_path, | ||
tmp_path=tmp_path, | ||
ffmpeg_args=ffmpeg_args, | ||
threads=threads, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters