Created
September 21, 2021 08:52
-
-
Save daltonbr/15c9add1ae60b175ac97fe3276dc125d to your computer and use it in GitHub Desktop.
Batch reencode video files with FFmpeg - version 1.0 - using python 3.9
This file contains 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 | |
# | |
# Batch file converter to reencode mp4 videos with a steady fixed i-frame interval (default to 25) | |
# | |
# Tested under python 3.9.7 | |
# Requires ffmpeg installed | |
# On Mac run 'brew install ffmpeg' | |
# | |
# You may need to give permission to this script | |
# $ chmod 700 ./iframe-steady.py | |
# | |
# Run this script from the same directory as your mp4 files | |
# | |
# e.g. | |
# $ ./iframe-steady.py | |
# | |
# This is main ffmpeg command run | |
# ffmpeg -i input.mp4 -c:v libx264 -preset slow -crf 22 -g 25 -keyint_min 25 -sc_threshold 0 -c:a copy output.mp4 | |
# | |
# check for a new 'exported_videos' folder | |
# | |
# [email protected] | |
# Know issue your files shouldn't have spaces our `.` (periods) in the file names. | |
# | |
print ('Make i-frames steadier') | |
import os, sys | |
directory = '.' | |
def is_tool(name): | |
"""Check whether `name` is on PATH and marked as executable.""" | |
from shutil import which | |
return which(name) is not None | |
# check if have ffmpeg | |
if is_tool('ffmpeg') == False: | |
sys.exit ("ffmpeg not found! Run 'brew install ffmpeg' and run this script again") | |
# create a new folder named sequentially | |
try: | |
i = 0 | |
new_folder = "exported_videos_%s" | |
while os.path.exists(new_folder % i): | |
i += 1 | |
new_folder = new_folder % i | |
os.makedirs(new_folder) | |
except OSError as e: | |
if e.errno != errno.EEXIST: | |
raise | |
# for each mp4 files in the dir, runs ffmpeg and output to that new folder | |
for filename in os.listdir(directory): | |
f = os.path.join(directory, filename) | |
if os.path.isfile(f): | |
if (f.endswith('.mp4')): | |
print(f + ' is a mp4 file') | |
os.system(f'ffmpeg -i {f} -c:v libx264 -preset slow -crf 22 -g 25 -keyint_min 25 -sc_threshold 0 -c:a copy {new_folder}/{f.removeprefix("./")}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment