-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_ffmpeg.py
More file actions
68 lines (56 loc) · 2.27 KB
/
install_ffmpeg.py
File metadata and controls
68 lines (56 loc) · 2.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import zipfile
import urllib.request
import sys
import shutil
FFMPEG_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
BIN_DIR = os.path.join(PROJECT_DIR, "bin")
def install_ffmpeg(target_dir=None):
if target_dir is None:
target_dir = BIN_DIR
print(f"Checking for FFmpeg in {target_dir}...")
if not os.path.exists(target_dir):
os.makedirs(target_dir)
if os.path.exists(os.path.join(target_dir, "ffmpeg.exe")):
print("✅ FFmpeg is already installed.")
return
zip_path = os.path.join(target_dir, "ffmpeg.zip")
print(f"⬇️ Downloading FFmpeg from {FFMPEG_URL}...")
try:
# Use a user-agent to avoid 403 errors
req = urllib.request.Request(
FFMPEG_URL,
data=None,
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
)
with urllib.request.urlopen(req) as response, open(zip_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
except Exception as e:
print(f"❌ Download failed: {e}")
return
print("📦 Extracting FFmpeg...")
try:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
# Find the bin folder inside the zip
# The zip usually has a structure like: ffmpeg-release-essentials/bin/ffmpeg.exe
for file in zip_ref.namelist():
if file.endswith('ffmpeg.exe'):
# Extract single file to target_dir
source = zip_ref.open(file)
target = open(os.path.join(target_dir, "ffmpeg.exe"), "wb")
with source, target:
shutil.copyfileobj(source, target)
print(f"Extracted: {file}")
except Exception as e:
print(f"❌ Extraction failed: {e}")
finally:
# Cleanup
if os.path.exists(zip_path):
os.remove(zip_path)
print("✅ FFmpeg installed successfully!")
if __name__ == "__main__":
if len(sys.argv) > 1:
install_ffmpeg(sys.argv[1])
else:
install_ffmpeg()