Spaces:
Sleeping
Sleeping
| 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() | |