Spaces:
Sleeping
Sleeping
Create download_movies.py
Browse files- download_movies.py +136 -0
download_movies.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
try:
|
| 7 |
+
import requests
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
except ImportError:
|
| 10 |
+
print("Install required packages: pip install requests tqdm")
|
| 11 |
+
sys.exit(1)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def check_ffmpeg():
|
| 15 |
+
"""Check if ffmpeg is installed."""
|
| 16 |
+
try:
|
| 17 |
+
result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 18 |
+
if result.returncode == 0:
|
| 19 |
+
print(f"ffmpeg available: {result.stdout.splitlines()[0]}")
|
| 20 |
+
return True
|
| 21 |
+
except FileNotFoundError:
|
| 22 |
+
return False
|
| 23 |
+
return False
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def install_ffmpeg():
|
| 27 |
+
"""Try auto-install ffmpeg (Windows only example with choco)."""
|
| 28 |
+
if os.name == "nt":
|
| 29 |
+
print("β Installing ffmpeg via Chocolatey (Windows)...")
|
| 30 |
+
subprocess.run("choco install ffmpeg -y", shell=True)
|
| 31 |
+
else:
|
| 32 |
+
print("β Please install ffmpeg manually for your OS.")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def download_file(url, output_path):
|
| 36 |
+
"""Download a file with tqdm progress bar."""
|
| 37 |
+
output_path = Path(output_path)
|
| 38 |
+
|
| 39 |
+
if output_path.exists():
|
| 40 |
+
print(f"β
File already exists: {output_path}")
|
| 41 |
+
return output_path
|
| 42 |
+
|
| 43 |
+
response = requests.get(url, stream=True)
|
| 44 |
+
total_size = int(response.headers.get("content-length", 0))
|
| 45 |
+
block_size = 1024
|
| 46 |
+
|
| 47 |
+
t = tqdm(total=total_size, unit="iB", unit_scale=True, desc="Downloading")
|
| 48 |
+
with open(output_path, "wb") as f:
|
| 49 |
+
for data in response.iter_content(block_size):
|
| 50 |
+
t.update(len(data))
|
| 51 |
+
f.write(data)
|
| 52 |
+
|
| 53 |
+
t.close()
|
| 54 |
+
|
| 55 |
+
if total_size != 0 and output_path.stat().st_size != total_size:
|
| 56 |
+
print("β ERROR: Download incomplete.")
|
| 57 |
+
else:
|
| 58 |
+
print("β
Download complete.")
|
| 59 |
+
|
| 60 |
+
return output_path
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def convert_to_mp4(input_file: Path, delete_original=True):
|
| 64 |
+
"""Convert MKV to MP4 using ffmpeg."""
|
| 65 |
+
|
| 66 |
+
# β
If already MP4, skip
|
| 67 |
+
if input_file.suffix.lower() == ".mp4":
|
| 68 |
+
print(f"β
Already MP4, skipping conversion: {input_file}")
|
| 69 |
+
return input_file
|
| 70 |
+
|
| 71 |
+
if input_file.suffix.lower() != ".mkv":
|
| 72 |
+
print(f"β Unsupported format ({input_file.suffix}), skipping conversion.")
|
| 73 |
+
return input_file
|
| 74 |
+
|
| 75 |
+
output_file = input_file.with_suffix(".mp4")
|
| 76 |
+
print(f"Converting {input_file} β {output_file}")
|
| 77 |
+
subprocess.run([
|
| 78 |
+
"ffmpeg", "-y",
|
| 79 |
+
"-i", str(input_file),
|
| 80 |
+
"-c:v", "copy",
|
| 81 |
+
"-c:a", "aac",
|
| 82 |
+
str(output_file)
|
| 83 |
+
])
|
| 84 |
+
print(f"β
Conversion complete: {output_file}")
|
| 85 |
+
|
| 86 |
+
if delete_original:
|
| 87 |
+
input_file.unlink(missing_ok=True)
|
| 88 |
+
print(f"ποΈ Deleted original file: {input_file}")
|
| 89 |
+
|
| 90 |
+
return output_file
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def convert_to_hls(input_file, segment_time: int = 10):
|
| 94 |
+
"""Convert MP4 to HLS (.m3u8 + .ts segments)."""
|
| 95 |
+
input_file = Path(input_file)
|
| 96 |
+
|
| 97 |
+
# Output directory
|
| 98 |
+
output_dir = input_file.with_suffix("")
|
| 99 |
+
output_dir.mkdir(exist_ok=True)
|
| 100 |
+
|
| 101 |
+
m3u8_file = output_dir / "output.m3u8"
|
| 102 |
+
|
| 103 |
+
print(f"Converting {input_file} β HLS ({segment_time}s segments) in {output_dir}")
|
| 104 |
+
subprocess.run([
|
| 105 |
+
"ffmpeg", "-y",
|
| 106 |
+
"-i", str(input_file),
|
| 107 |
+
"-c:v", "libx264",
|
| 108 |
+
"-crf", "23",
|
| 109 |
+
"-preset", "fast",
|
| 110 |
+
"-c:a", "aac",
|
| 111 |
+
"-b:a", "128k",
|
| 112 |
+
"-ac", "2",
|
| 113 |
+
"-hls_time", str(segment_time),
|
| 114 |
+
"-hls_playlist_type", "vod",
|
| 115 |
+
str(m3u8_file)
|
| 116 |
+
], check=True)
|
| 117 |
+
|
| 118 |
+
print(f"β
HLS conversion complete: {m3u8_file}")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def main(output, url, delete_original=False):
|
| 122 |
+
print("LEGAL: Use this only for authorized/public-domain content.")
|
| 123 |
+
|
| 124 |
+
if not check_ffmpeg():
|
| 125 |
+
install_ffmpeg()
|
| 126 |
+
|
| 127 |
+
filename = output if output else os.path.basename(url.split("?")[0])
|
| 128 |
+
file_path = download_file(url, filename)
|
| 129 |
+
|
| 130 |
+
# Convert MKV β MP4 (skip if already mp4)
|
| 131 |
+
mp4_file = convert_to_mp4(file_path, delete_original=delete_original)
|
| 132 |
+
|
| 133 |
+
# Convert MP4 β HLS
|
| 134 |
+
convert_to_hls(mp4_file, segment_time=10)
|
| 135 |
+
|
| 136 |
+
print("β
All tasks completed.")
|