diff --git a/src/zimscraperlib/video/presets.py b/src/zimscraperlib/video/presets.py index 7513c27d..d2fda937 100644 --- a/src/zimscraperlib/video/presets.py +++ b/src/zimscraperlib/video/presets.py @@ -6,6 +6,12 @@ class VoiceMp3Low(Config): + """ Low quality mp3 audio + + 44Khz audio sampling + 48k audio bitrate + audio-only """ + VERSION = 1 options = { @@ -17,6 +23,13 @@ class VoiceMp3Low(Config): class VideoWebmLow(Config): + """ Low Quality webm video + + 480:h format with height adjusted to keep aspect ratio + 300k video bitrate + 48k audio bitrate + highly degraded quality (30, 42) """ + VERSION = 1 options = { @@ -24,12 +37,36 @@ class VideoWebmLow(Config): "-quality": "best", # codec preset "-b:v": "300k", # target video bitrate "-maxrate": "300k", # max video bitrate - "-bufsize": "1000k", # buffer size "-minrate": "300k", # min video bitrate + "-qmin": "30", # min quantizer scale + "-qmax": "42", # max quantizer scale + "-vf": "scale='480:trunc(ow/a/2)*2'", # frame size "-codec:a": "libvorbis", # audio codec + "-ar": "44100", # audio sampling rate + "-b:a": "48k", # target audio bitrate + } + + +class VideoMp4Low(Config): + """ Low Quality mp4 video + + 480:h format with height adjusted to keep aspect ratio + 300k video bitrate + 48k audio bitrate + highly degraded quality (30, 42) """ + + VERSION = 1 + + options = { + "-codec:v": "h264", # video codec + "-b:v": "300k", # target video bitrate + "-maxrate": "300k", # max video bitrate + "-minrate": "300k", # min video bitrate "-qmin": "30", # min quantizer scale "-qmax": "42", # max quantizer scale "-vf": "scale='480:trunc(ow/a/2)*2'", # frame size + "-codec:a": "aac", # audio codec "-ar": "44100", # audio sampling rate "-b:a": "48k", # target audio bitrate + "-movflags": "+faststart", # extra flag } diff --git a/tests/video/test_video.py b/tests/video/test_video.py index 0647c194..f126320b 100644 --- a/tests/video/test_video.py +++ b/tests/video/test_video.py @@ -9,7 +9,7 @@ import shutil from zimscraperlib.video.config import Config -from zimscraperlib.video.presets import VoiceMp3Low, VideoWebmLow +from zimscraperlib.video.presets import VoiceMp3Low, VideoWebmLow, VideoMp4Low from zimscraperlib.video.encoding import reencode from zimscraperlib.video.probing import get_media_info @@ -121,14 +121,13 @@ def test_preset_video_webm_low(): config = VideoWebmLow() assert config.VERSION == 1 args = config.to_ffmpeg_args() - assert len(args) == 26 + assert len(args) == 24 options_map = [ ("codec:v", "libvpx"), ("codec:a", "libvorbis"), ("maxrate", "300k"), ("minrate", "300k"), ("b:v", "300k"), - ("bufsize", "1000k"), ("ar", "44100"), ("b:a", "48k"), ("quality", "best"), @@ -151,6 +150,39 @@ def test_preset_video_webm_low(): assert idx != -1 and args[idx + 1] == "900k" +def test_preset_video_mp4_low(): + config = VideoMp4Low() + assert config.VERSION == 1 + args = config.to_ffmpeg_args() + assert len(args) == 24 + options_map = [ + ("codec:v", "h264"), + ("codec:a", "aac"), + ("maxrate", "300k"), + ("minrate", "300k"), + ("b:v", "300k"), + ("ar", "44100"), + ("b:a", "48k"), + ("movflags", "+faststart"), + ("qmin", "30"), + ("qmax", "42"), + ("vf", "scale='480:trunc(ow/a/2)*2'"), + ] + for option, val in options_map: + idx = args.index(f"-{option}") + assert idx != -1 + assert args[idx + 1] == val + + # test updating values + config = VideoMp4Low(**{"-ar": "50000"}) + config["-bufsize"] = "900k" + args = config.to_ffmpeg_args() + idx = args.index("-ar") + assert idx != -1 and args[idx + 1] == "50000" + idx = args.index("-bufsize") + assert idx != -1 and args[idx + 1] == "900k" + + def test_preset_voice_mp3_low(): config = VoiceMp3Low() assert config.VERSION == 1