1+ import argparse
12from pathlib import Path
23import subprocess
34
4- ffmpeg_path = Path ("." , "ffmpeg-2024-12-09" , "bin" , "ffmpeg" )
5- ffprobe_path = Path ("." , "ffmpeg-2024-12-09" , "bin" , "ffprobe" )
6-
7- in_path = Path ("." , "in" )
8- in_path .mkdir (parents = True , exist_ok = True )
9-
10- out_path = Path ("." , "out" )
11- out_path .mkdir (parents = True , exist_ok = True )
12-
13- convered = 0
14-
15- videos = Path ("." , "in" ).rglob ('*.mp4' )
16- for video in videos :
17-
18- size = subprocess .run ([
19- ffprobe_path ,
20- "-v" , "error" ,
21- "-select_streams" , "v:0" ,
22- "-show_entries" , "stream=width,height" ,
23- "-of" , "csv=s=x:p=0" ,
24- video
25- ], capture_output = True , text = True ).stdout
26- width , height = size .strip ().split ("x" )
27-
28- webp_path = Path (out_path , f"{ video .name .replace (".mp4" , "" )} .webp" )
29- return_code = subprocess .run ([
30- ffmpeg_path ,
31- "-i" , video ,
32- "-vcodec" , "libwebp" ,
33- "-filter:v" , "fps=fps=20" ,
34- "-lossless" , "1" ,
35- "-loop" , "0" ,
36- "-preset" , "default" ,
37- "-an" ,
38- "-vsync" , "0" ,
39- "-s" , f"{ width } :{ height } " ,
40- "-y" , # overwrite
41- webp_path
42- ])
43-
44- print (return_code )
45-
46- convered += 1
47-
48- print (f"\n converted { convered } videos to webp!" )
5+ parser = argparse .ArgumentParser (
6+ prog = "martin" ,
7+ description = "batch convert mp4 to webp using ffmpeg"
8+ )
9+ parser .add_argument ('--fps' , default = 25 , required = False )
10+ args = parser .parse_args ()
11+
12+ def run ():
13+ ffmpeg_path = Path ("." , "ffmpeg-2024-12-09" , "bin" , "ffmpeg" )
14+ ffprobe_path = Path ("." , "ffmpeg-2024-12-09" , "bin" , "ffprobe" )
15+
16+ in_path = Path ("." , "in" )
17+ in_path .mkdir (parents = True , exist_ok = True )
18+
19+ out_path = Path ("." , "out" )
20+ out_path .mkdir (parents = True , exist_ok = True )
21+
22+ convered = 0
23+
24+ videos = Path ("." , "in" ).rglob ('*.mp4' )
25+ for video in videos :
26+
27+ size = subprocess .run ([
28+ ffprobe_path ,
29+ "-v" , "error" ,
30+ "-select_streams" , "v:0" ,
31+ "-show_entries" , "stream=width,height" ,
32+ "-of" , "csv=s=x:p=0" ,
33+ video
34+ ], capture_output = True , text = True ).stdout
35+ width , height = size .strip ().split ("x" )
36+
37+ webp_path = Path (out_path , f"{ video .name .replace (".mp4" , "" )} .webp" )
38+ return_code = subprocess .run ([
39+ ffmpeg_path ,
40+ "-i" , video ,
41+ "-vcodec" , "libwebp" ,
42+ "-filter:v" , f"fps=fps={ args .fps } " ,
43+ "-lossless" , "1" ,
44+ "-loop" , "0" ,
45+ "-preset" , "default" ,
46+ "-an" ,
47+ "-vsync" , "0" ,
48+ "-s" , f"{ width } :{ height } " ,
49+ "-y" , # overwrite
50+ webp_path
51+ ])
52+
53+ print (return_code )
54+
55+ convered += 1
56+
57+ print (f"\n converted { convered } videos to webp!" )
58+
59+ if __name__ == "__main__" :
60+ run ()
0 commit comments