Created
April 7, 2025 21:01
-
-
Save basperheim/4547c5dd62270f6f373d0850b08ac052 to your computer and use it in GitHub Desktop.
Converts Apple QuickTime Videos to MP4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, time | |
import subprocess | |
from pathlib import Path | |
# Constants | |
CRF = "23" | |
PRESET = "medium" | |
AUDIO_BITRATE = "192k" | |
MIN_FILE_SIZE_MB = 10 | |
NUM_THREADS = "4" | |
def convert_mov_to_mp4(): | |
current_dir = Path(".") | |
mov_files = [ | |
f for f in current_dir.iterdir() | |
if f.suffix.lower() == ".mov" and f.stat().st_size > MIN_FILE_SIZE_MB * 1024 * 1024 | |
] | |
if not mov_files: | |
print("No eligible .MOV files found.") | |
return | |
for mov_file in mov_files: | |
output_file = mov_file.with_suffix(".mp4") | |
cmd = [ | |
"ffmpeg", | |
"-hide_banner", | |
"-loglevel", "error", | |
"-y", | |
"-i", str(mov_file), | |
"-c:v", "libx264", | |
"-crf", CRF, | |
"-preset", PRESET, | |
"-threads", NUM_THREADS, | |
"-c:a", "aac", | |
"-b:a", AUDIO_BITRATE, | |
str(output_file) | |
] | |
print(f"\nConverting: {mov_file} → {output_file}") | |
print(' '.join(cmd)) | |
start_time = time.time() | |
try: | |
# Launch the process | |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
# Poll process and print elapsed time | |
while True: | |
if process.poll() is not None: | |
break | |
elapsed = int(time.time() - start_time) | |
print(f"\r⏱️ {elapsed} seconds elapsed...", end='', flush=True) | |
time.sleep(5) | |
stdout, stderr = process.communicate() | |
if process.returncode == 0: | |
total_time = int(time.time() - start_time) | |
print(f"\r✅ Success: {mov_file.name} ({total_time} seconds)") | |
else: | |
print(f"\r❌ Error converting {mov_file.name}") | |
print("stderr:", stderr) | |
except Exception as e: | |
print(f"\r❌ Exception while converting {mov_file.name}: {e}") | |
convert_mov_to_mp4() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment