Skip to content

Instantly share code, notes, and snippets.

Last active Mar 23, 2021
What would you like to do?
Convert a video file's audio so it's Chromecast compatible
#!/usr/bin/env python3
# License: AGPLv3
# Copyright 2021 Alex DeLorenzo
# Requires Python 3.10+ and ffmpeg
from typing import Final
from subprocess import run
from pathlib import Path
from sys import argv
from os import cpu_count
import logging
NEW_SUFFIX: Final[str] = '_castconvert'
MP3_QUALITY: Final[int] = 1 # MP3 V1
THREADS: Final[int] = cpu_count() or 1
CMD_FMT: Final[str] = (
f'ffmpeg -y -fflags +genpts'
f' -i "{{orig}}"'
f' -c:a libmp3lame -q:a {MP3_QUALITY}'
f' -c:v copy -threads {THREADS}'
f' "{{new}}"'
def convert_audio(vid: Path) -> Path:
if NEW_SUFFIX in str(vid):"Already converted: {vid}")
return vid
vid = vid.absolute()
new_stem: str = vid.stem + NEW_SUFFIX
new_vid: Path = vid.with_stem(new_stem)
new_vid = new_vid.absolute()
if new_vid.exists():"Already converted: {new_vid}")
return new_vid
cmd: str = CMD_FMT.format(
run(cmd, shell=True)
return new_vid
def main():
vids: list[str]
_, *vids = argv
for vid in vids:
path = Path(vid)
new_path = convert_audio(path)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment