Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/zimscraperlib/video/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@


class VoiceMp3Low(Config):
""" Low quality mp3 audio

44Khz audio sampling
48k audio bitrate
audio-only """

VERSION = 1

options = {
Expand All @@ -17,19 +23,50 @@ 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 = {
"-codec:v": "libvpx", # video codec
"-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
}
38 changes: 35 additions & 3 deletions tests/video/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"),
Expand All @@ -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
Expand Down