Back

ffmpeg batch compression conversion video to 30FPS

Use ffmpeg to compress large videos into small videos

Table of contents

TL;DR / [Geek Summary]:

  • Automation Pipeline: Craft Python scripts to drive FFmpeg, traversing directories for one-click batch video compression.
  • Parameter Alchemy: Lock frame rates to 30FPS and tune bitrates (-b:v 2000k) to balance output quality and file size.
  • Environment Optimized: Specifically tuned for mobile Linux environments like Termux, turning your phone into a media processing beast.

# 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

转载需要保留原始链接,未经明确许可,禁止商业使用。CC BY-NC-SA 4.0

Last updated on 2026-05-16 05:05 UTC