Last active 1 month ago

Revision 3477f983458bf47a57b561fead6ecf673fb4dd39

VideoConvert.py Raw
1import sys
2import os
3import subprocess
4from pathlib import Path
5
6# --- PyInstaller & Path Handling ---
7if getattr(sys, 'frozen', False):
8 # Running as a compiled PyInstaller executable
9 EXE_DIR = Path(sys.executable).parent
10 BUNDLE_DIR = Path(sys._MEIPASS)
11else:
12 # Running as a standard Python script
13 EXE_DIR = Path(__file__).parent
14 BUNDLE_DIR = EXE_DIR
15
16# Locate the bundled ffmpeg.exe
17FFMPEG_EXE = BUNDLE_DIR / "ffmpeg.exe"
18
19# If ffmpeg.exe isn't bundled inside, fallback to system PATH
20if not FFMPEG_EXE.exists():
21 FFMPEG_EXE = "ffmpeg"
22
23# Settings
24TARGET_WIDTH = 1280
25TARGET_HEIGHT = 720
26TARGET_FPS = 24
27
28# Process videos relative to where the executable/script resides
29input_folder = EXE_DIR
30output_folder = input_folder / "converted"
31output_folder.mkdir(exist_ok=True)
32
33mp4_files = list(input_folder.glob("*.mp4"))
34
35if not mp4_files:
36 print("No MP4 files found.")
37 input("Press Enter to exit...")
38 sys.exit()
39
40for video in mp4_files:
41 output_file = output_folder / video.name
42
43 print(f"Converting: {video.name}")
44
45 cmd = [
46 str(FFMPEG_EXE), # Points directly to bundled ffmpeg.exe
47 "-y",
48 "-i", str(video),
49 "-vf",
50 f"scale={TARGET_WIDTH}:{TARGET_HEIGHT}:force_original_aspect_ratio=decrease,pad={TARGET_WIDTH}:{TARGET_HEIGHT}:(ow-iw)/2:(oh-ih)/2",
51 "-r", str(TARGET_FPS),
52 "-c:v", "libx264",
53 "-preset", "veryfast",
54 "-crf", "25",
55 "-pix_fmt", "yuv420p",
56 "-c:a", "aac",
57 "-b:a", "128k",
58 str(output_file)
59 ]
60
61 result = subprocess.run(cmd)
62
63 if result.returncode == 0:
64 print(f"✓ Finished {video.name}")
65 else:
66 print(f"✗ Failed {video.name}")
67
68print("\nDone!")
69input("Press Enter to exit...")