A Python 3 GUI application to download YouTube videos and/or audio with separate progress bars for audio, video, and processing tasks. Built using Tkinter and pytubefix, with FFmpeg support for merging audio and video streams.
- Download audio-only, video-only, or both (merged).
- Supports progressive and adaptive streams up to 4K resolution.
- Separate progress bars for:
- Audio download
- Video download
- Processing (converting or merging)
- Fetch available resolutions dynamically from the video URL.
- User-friendly GUI with Tkinter.
- Cross-platform: Works on Windows, macOS, and Linux.
- Automatically sanitizes filenames to avoid illegal characters.
- Option to open the containing folder after download completes.
| Library | Purpose | Notes / Features |
|---|---|---|
pytubefix |
Handles YouTube video fetching and downloading, including adaptive streams | A fork of pytube that fixes current YouTube API issues |
Tkinter |
GUI framework for Python | Provides windows, buttons, entries, labels, progress bars |
ttk |
Styled Tkinter widgets (Progressbar, Combobox) | Used for better GUI appearance |
subprocess |
Runs FFmpeg commands for audio/video conversion and merging | Required to merge video + audio and convert formats |
queue |
Thread-safe communication between worker threads and UI thread | Ensures safe updates to Tkinter widgets from background threads |
threading |
Handles download tasks in background threads | Prevents GUI from freezing during downloads |
os / sys |
File path handling, OS-specific folder opening | Required for cross-platform file handling |
- URL Input: User enters a YouTube URL.
- Fetch Resolutions: Clicking "Fetch Resolutions" queries available video resolutions.
- Select Options: User can choose:
- Resolution (e.g., 1080p, 720p)
- Video format (
mp4,webm) - Audio format (
m4a,mp3,webm) - Download type (
video,audio, orboth)
- Download Process:
- Audio-only: Downloads the highest bitrate audio stream, converts it with FFmpeg.
- Video-only: Downloads the selected resolution video stream.
- Both: Downloads audio + video separately, then merges using FFmpeg.
- Progress Bars:
- Audio bar tracks audio download.
- Video bar tracks video download.
- Processing bar shows FFmpeg conversion/merge progress.
- Completion:
- Displays a message with file path.
- Option to open the containing folder.
Tkinter is the standard GUI library for Python. Key components used in this project:
- Widgets: Basic GUI elements like
Label,Entry,Button,Progressbar, andCombobox. - Grid Layout: Positions widgets in a table-like grid using
rowandcolumn. Example:
Label(root, text="YouTube URL:").grid(row=0, column=0, sticky=E)
Entry(root, textvariable=url_var).grid(row=0, column=1, columnspan=3, sticky=W)-
StringVar / DoubleVar: Special Tkinter variables that automatically update widgets when their value changes. Examples:
StringVarfor text (URL, status, selected resolution)DoubleVarfor numeric values (progress bars)
progress_var = DoubleVar(value=0.0)
Progressbar(root, variable=progress_var, maximum=100)
progress_var.set(50.0) # Updates bar visuallyDownloading video/audio can block the GUI. To prevent freezing:
- Threading: Run download tasks in a separate background thread.
- Queue: Safe communication channel from the background thread to the main GUI thread.
ui_queue = queue.Queue()
threading.Thread(target=download_worker, args=(url,), daemon=True).start()- Worker thread puts messages in the queue (
ui_queue.put(...)). - GUI polls the queue periodically (
root.after(100, _process_ui_queue)), updating progress bars and status safely.
Subprocess allows executing system commands from Python:
- Used for audio conversion and merging audio/video.
- Example of converting audio:
ffmpeg_cmd = ["ffmpeg", "-y", "-i", "temp_audio.m4a", "final_audio.mp3"]
subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)- Example of merging audio + video:
ffmpeg_cmd = [
"ffmpeg", "-y",
"-i", "video.mp4",
"-i", "audio.m4a",
"-c:v", "copy", "-c:a", "aac",
"output.mp4"
]stdoutandstderrare captured to prevent cluttering the console.
YouTube videos are available in different stream types:
- Progressive Streams: Contain both video and audio in one file. Easier to download but often limited in resolution.
- Adaptive Streams: Separate audio-only and video-only streams. Allows high-resolution video (4K+) but requires merging using FFmpeg.
Example using pytubefix:
yt.streams.filter(progressive=True, file_extension="mp4") # video+audio
yt.streams.filter(only_video=True, file_extension="mp4") # video only
yt.streams.filter(only_audio=True).order_by("abr").desc() # audio only- Cross-platform paths: Use
os.path.join()instead of hardcoding slashes. - Sanitize filenames to avoid illegal characters:
def sanitize_filename(name):
return "".join(c if c.isalnum() or c in " ._-()" else "_" for c in name).strip()- Open folder after download depending on OS:
if sys.platform == "win32": os.startfile(folder)
elif sys.platform == "darwin": subprocess.Popen(["open", folder])
else: subprocess.Popen(["xdg-open", folder])FFmpeg is a command-line tool for audio/video processing:
- Merging audio + video:
ffmpeg -i video.mp4 -i audio.m4a -c:v copy -c:a aac output.mp4- Converting audio format:
ffmpeg -i temp_audio.m4a final_audio.mp3- Options Used in Code:
-y: Overwrite output without asking-loglevel error: Only show errors-c:v copy: Copy video stream without re-encoding (fast)-c:a aac: Encode audio to AAC format for MP4 compatibility
- Clone the repo:
git clone https://github.com/fitandfine/youtube_downloader.git
cd youtube_downloader- Install dependencies:
pip install pytubefixTkinter is included with Python; install via OS package manager if missing.
- Install FFmpeg:
- Ubuntu:
sudo apt install ffmpeg - Windows: Download FFmpeg and add to PATH see here: https://www.wikihow.com/Install-FFmpeg-on-Windows
- macOS:
brew install ffmpeg
python app.py- Enter YouTube URL.
- Fetch resolutions.
- Choose resolution, video/audio format, and download type.
- Select save folder.
- Start download.
- Watch separate progress bars for audio, video, and processing.
- Open folder when prompted.
Anup Chapain – Created with clarity, separate progress bars, and comments for maintainability.
MIT License – free to use and modify.