ffmpeg batch conversion to small videos
Sometimes we need to batch compress the videos in the folder. At this time, we can use python to automatically control ffmpeg to realize batch conversion. This script has only been used on termux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| import os
import subprocess
import shlex
script_dir = os.path.dirname(os.path.realpath(__file__))
video_dir = script_dir
for root, dirs, files in os.walk(video_dir):
for videos in files:
if video.endswith('.mp4'):
filename, ext = os.path.splitext(video)
out_file1 = filename + '_1' + ext
cmd1 = f'ffmpeg -i {shlex.quote(os.path.join(root, video))} -g 90 -b:v 2000k -bufsize 2000k -maxrate 2500k {shlex.quote(os.path.join(root, out_file1))}'
subprocess.run(cmd1, shell=True)
out_file2 = filename + '_2' + ext
cmd2 = f'ffmpeg -i {shlex.quote(os.path.join(root, out_file1))} -g 90 -r 30 {shlex.quote(os.path.join(root, out_file2))}'
subprocess.run(cmd2, shell=True)
os.remove(os.path.join(root, out_file1))
print('Done!')
|
How to use
Install ffmpeg and python3 and run