Last active
November 5, 2025 05:24
-
-
Save georgy7/353abe2b1f4bd49a20f06bb3a44c954d to your computer and use it in GitHub Desktop.
It scales down MP4 files to 720p.
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Task: to reduce the resolution of all videos to 720p in a folder without delving into the subdirectories. | |
| If a video is already in low resolution, the script just copies it. | |
| Usage: | |
| Run it in the video folder and specify as an argument | |
| the folder where you want to save the result. | |
| cd some_folder | |
| python3 path/to/the/script.py output_folder/ | |
| License: WTFPL | |
| """ | |
| from argparse import ArgumentParser | |
| from pathlib import Path | |
| import sys | |
| import os | |
| import shutil | |
| import subprocess | |
| def main(): | |
| parser = ArgumentParser(prog='scale_down_all.py', | |
| description="It scales down all MP4 of the current directory to 720p.") | |
| parser.add_argument('destination') | |
| args = parser.parse_args() | |
| output = Path(args.destination).expanduser() | |
| if not output.is_dir(): | |
| print("The destination folder does not exists. Please create it and try again.") | |
| sys.exit(1) | |
| input_folder = Path.cwd() | |
| frame_extensions = ["mp4"] | |
| file_list = [ | |
| file_path | |
| for file_path in input_folder.iterdir() | |
| if file_path.is_file() | |
| and file_path.suffix[1:].lower() in frame_extensions | |
| ] | |
| get_h_opts = 'ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0' | |
| ffmpeg_main_options = '-vf scale=-2:720 -c:v libx264 -preset fast -tune fastdecode -crf 28 -pix_fmt yuv420p'.split(" ") | |
| for f in file_list: | |
| height = int(os.popen(get_h_opts + " \"{}\"".format(f.name)).read()) | |
| if height <= 720: | |
| shutil.copy(f, output) | |
| print("{}: COPIED (h={})".format(f.name, height)) | |
| else: | |
| ffmpeg_options = ["ffmpeg", "-i", str(f.name)] + ffmpeg_main_options + [str(output / f.name)] | |
| subprocess.check_call(ffmpeg_options) | |
| print("{}: OK".format(f.name)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment