Created
October 10, 2021 07:12
-
-
Save hkusoft/1e285ec69cce654fe7a4f2e172d366c2 to your computer and use it in GitHub Desktop.
Batch resize mp4 files down to 100M or below using FFmpeg
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
import glob | |
import os | |
import sys | |
def get_all_mp4s(folder): | |
pattern = folder + '/**/*.mp4' | |
files = glob.glob(pattern, recursive=True) | |
return files | |
def convert(files): | |
cmd = "ffmpeg.exe" | |
for f in files: | |
src = os.path.normpath(f) | |
size = os.path.getsize(src); | |
if size >= 100 * 1024 * 1024: | |
target = src[:-4] + '_2' + src[-4:] | |
args = f' -i \"{src}\" -fs 100M \"{target}\"' | |
print("------------------------------\n") | |
os.system(f'{cmd}{args}') | |
os.replace(target, src) | |
print(f"---------- Done for {src}\n") | |
if __name__ == '__main__': | |
folder = ' '.join(sys.argv[1:]) | |
# folder = sys.argv[1] | |
files = get_all_mp4s(folder) | |
convert(files) | |
# python main.py your/folder/directory/here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment