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