Skip to content

Instantly share code, notes, and snippets.

@cast42
Created May 12, 2023 09:17
Show Gist options
  • Save cast42/85b2aa1b05a47af8327a232ff69f902e to your computer and use it in GitHub Desktop.
Save cast42/85b2aa1b05a47af8327a232ff69f902e to your computer and use it in GitHub Desktop.
Rescale all mp4 files in --source-folder to 320 lines while keeping the source aspect ratio with ffmpeg and place rescaled videos in --destination-folder
"""Rescale all mp4 files in --source-folder to 320 lines while keeping the source aspect ratio
with ffmpeg and place rescaled videos in --destination-folder"""
import argparse
import subprocess
import logging
from datetime import datetime
from pathlib import Path
if __name__ == "__main__":
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser()
parser.add_argument(
"--source-folder",
type=str,
required=False,
default="/Users/user/default/input/directory",
help="The folder containing the video files to rescale.",
)
parser.add_argument(
"--destination-folder",
type=str,
required=False,
default="/Users/user/default/output/directory",
help="The folder to save the dwonscaled videos in. Will be created if it doesn't exist yet.",
)
args = parser.parse_args()
src = Path(args.source_folder)
dst = Path(args.destination_folder)
dst.mkdir(exist_ok=True)
for file in src.glob("*.mp4"):
newfile = dst / file.name
try:
subprocess.run(
[
"/usr/local/bin/ffmpeg",
"-y",
"-i",
file.as_posix(),
"-vf",
"scale=320:-2,setsar=1:1",
"-c:v",
"libx264",
"-r",
"30",
"-hide_banner",
"-loglevel",
"error",
newfile.as_posix(),
]
)
except Exception as e:
logger.error(f"Error while transcoding with ffmpeg {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment