File size: 1,221 Bytes
1f052c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import subprocess
from tqdm import tqdm

def is_video_file(filename):
    video_exts = ('.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv')
    return filename.lower().endswith(video_exts)

def collect_video_files(folder):
    video_files = []
    for dirpath, _, filenames in os.walk(folder):
        for filename in filenames:
            if is_video_file(filename):
                video_files.append(os.path.join(dirpath, filename))
    return video_files

def compress_video_ffmpeg(input_path, output_path, crf=24):
    tmp_output = input_path + ".tmp.mp4"
    cmd = [
        "ffmpeg", "-y", "-i", input_path,
        "-c:v", "libx264", "-preset", "veryslow", "-crf", str(crf),
        "-c:a", "aac", "-b:a", "128k",
        tmp_output
    ]
    subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    os.replace(tmp_output, output_path)

def main():
    lia_folder = os.path.join(os.path.dirname(__file__), "common_videos", "mcnet")
    video_files = collect_video_files(lia_folder)
    for video_path in tqdm(video_files, desc="Compressing videos"):
        compress_video_ffmpeg(video_path, video_path)
        print(f"Compressed {video_path}")

if __name__ == "__main__":
    main()