yt-dlp-script/yt_to_mp4.py

51 lines
1.9 KiB
Python
Raw Normal View History

2024-10-27 06:21:32 +00:00
import subprocess
import os
from rich.console import Console
from rich.panel import Panel
console = Console()
subprocess.run(['toilet', '-f', 'smslant', 'DREADFUL YT2MP4', '--gay'])
console.print(Panel("[bold green]Credits: Klez A.K.A Boomer", title="Credits"))
video_id = input("Enter the YouTube video ID: ").strip()
compressed_file = input("Enter the name for the compressed file (without .mp4): ").strip()
# Ensure the filename ends with '.mp4'
if not compressed_file.lower().endswith('.mp4'):
compressed_file += '.mp4'
url = f"https://www.youtube.com/watch?v={video_id}"
download_file = "video.mp4"
try:
console.print(f"[bold blue]Downloading video from {url}...", style="bold cyan")
subprocess.run(['yt-dlp', '-f', 'mp4', url, '-o', download_file], check=True)
console.print("[bold yellow]Compressing video to approximately 14MB...", style="bold cyan")
subprocess.run(
['ffmpeg', '-i', download_file, '-b:v', '500k', '-c:a', 'aac', '-b:a', '64k', '-preset', 'medium', '-pass', '1', '-f', 'mp4', '/dev/null'],
check=True
)
subprocess.run(
['ffmpeg', '-i', download_file, '-b:v', '500k', '-c:a', 'aac', '-b:a', '64k', '-preset', 'medium', '-pass', '2', compressed_file],
check=True
)
finally:
# Cleanup temporary files
if os.path.exists(download_file):
os.remove(download_file)
console.print(f"[bold red]Removed temporary file: {download_file}", style="bold cyan")
# Cleanup FFmpeg temporary files
for log_file in ['ffmpeg2pass-0.log', 'ffmpeg2pass-0.log.mbtree']:
if os.path.exists(log_file):
os.remove(log_file)
console.print(f"[bold red]Removed temporary file: {log_file}", style="bold cyan")
# Final output message
if os.path.exists(compressed_file):
console.print(f"[bold green]Final output file: {compressed_file}", style="bold cyan")