Mastering yt-dlp & FFmpeg for Web Deployment
yt-dlp is a feature-rich command-line media downloader. It is a fork of the original youtube-dl project, optimized for higher speeds and better compatibility with modern streaming protocols like DASH and HLS.
To run a yt-dlp website on the free tier, you need three files in your repository:
sdk: gradio and python_version: 3.11.ffmpeg to handle video/audio merging.yt-dlp and gradio.High-quality web video is often served as separate Video-only and Audio-only streams. Without FFmpeg, your app is limited to low-quality legacy formats (usually 720p or lower).
FFmpeg acts as the "Muxer," joining these streams into a single MP4 or MKV file after the download is complete.
Below is a production-ready app.py for a Hugging Face Space. It uses Gradio for the UI and yt-dlp for the backend.
import gradio as gr
import yt_dlp
import os
def download_video(url):
# Setup options with FFmpeg integration
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': 'downloads/%(title)s.%(ext)s',
# Ensure FFmpeg is used to merge streams
'postprocessors': [{
'key': 'FFmpegVideoConvertor',
'preferedformat': 'mp4',
}],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info).replace('.webm', '.mp4').replace('.mkv', '.mp4')
return filename
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🚀 Private Video Downloader")
url_input = gr.Textbox(label="Paste Link (YouTube, TikTok, Instagram, etc.)")
btn = gr.Button("Fetch Video", variant="primary")
output_file = gr.File(label="Download Ready")
btn.click(fn=download_video, inputs=url_input, outputs=output_file)
demo.launch()