Java Application Architecture
Overview
The YoutubeDownloader is a command-line Java application built with Maven that handles the actual downloading of YouTube videos. It processes video lists collected by the Chrome extension and downloads them with user-specified quality settings and folder organization. The application is designed around a modular architecture with clear separation of concerns.
Project Structure
Maven Configuration (pom.xml
)
The project uses Maven for dependency management and build automation:
<groupId>com.joseph</groupId>
<artifactId>YoutubeDownloader</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
Key Features:
- Java 11 Compatibility: Targets Java 11 for modern language features
- Shade Plugin: Creates a fat JAR with all dependencies included
- Main Class Configuration: Automatically sets the entry point to
com.joseph.youtube.YoutubeDownloader
- UTF-8 Encoding: Ensures proper character handling across platforms
Core Architecture Components
1. Main Application Class (YoutubeDownloader.java
)
The central orchestrator that provides the command-line interface and coordinates all operations:
Interactive Menu System
public void start(){
while (true) {
System.out.println("YouTube Video Downloader");
System.out.println("1. One video MP4 format and choose resolution");
System.out.println("2. MP3 format");
System.out.println("3. Download full youtube playlist and choose resolution");
System.out.println("4. Download list of videos from text file and choose resolution");
System.out.println("5. Change Configuration Settings");
// Handle user input...
}
}
Core Responsibilities:
- User Interface: Provides interactive command-line menu system
- Workflow Orchestration: Coordinates between configuration, file I/O, and download operations
- External Process Management: Executes youtube-dl commands and processes output
- History Tracking: Maintains download history to prevent duplicate downloads
- Error Handling: Manages failures and provides user feedback
Download Processing Pipeline
The application follows a sophisticated pipeline for processing downloads:
- URL Parsing: Extracts video IDs and parameters from YouTube URLs
- Quality Selection: Determines optimal video/audio format combinations
- Folder Management: Creates directory structures based on categories
- Command Construction: Builds youtube-dl commands with appropriate parameters
- Execution Monitoring: Tracks download progress and handles errors
2. Video Data Model (Video.java
)
A data transfer object that represents YouTube video metadata:
public class Video {
private String id; // YouTube video ID (11 characters)
private String folder; // Category/folder name
private String quality; // Desired quality (360p, 720p, etc.)
public static Video fromURL(String url) {
String[] arr = url.split("\\?");
Map<String, String> params = getParams(arr[1]);
String folder = params.get("f");
String quality = params.get("q");
String id = params.get("v");
return new Video(id, folder, quality);
}
}
Key Features:
- URL Parsing: Extracts video metadata from formatted URLs
- Parameter Processing: Handles both semicolon (
;
) and ampersand (&
) separators - Data Encapsulation: Provides clean access to video properties
- Validation: Ensures proper video ID format and parameter extraction
3. Configuration Management (Configuration.java
)
Handles application settings and user preferences:
Properties-Based Configuration
private void loadConfigFile(){
config = new Properties();
File file = new File(configFile);
if (!file.exists()) {
// Create default configuration
config.setProperty("destinationFolderName", destinationFolderName);
config.setProperty("downloadedFileName", downloadedFileName);
config.store(output, "Default configuration");
}
// Load existing configuration...
}
Configuration Parameters:
- destinationFolderName: Base directory for downloaded videos
- downloadedFileName: Template for naming downloaded files using youtube-dl variables
- downloadOnlyNewVideos: Boolean flag to skip previously downloaded videos
- defaultListFile: Default file path for batch processing
Dynamic Configuration Updates
The system provides an interactive configuration menu that allows users to:
- View current settings
- Modify individual configuration values
- Save changes persistently
- Reload configuration without restarting
4. File I/O Operations (FileIO.java
)
Utility class for file operations with robust error handling:
public static ArrayList<String> read(String fp){
File file = new File(fp);
ArrayList<String> contents = new ArrayList<String>();
try{
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
String line = sc.nextLine();
if(line.equals("")) continue; // Skip empty lines
contents.add(line);
}
} catch(FileNotFoundException ignored){
System.out.println("file not found : "+fp);
}
return contents;
}
Capabilities:
- File Reading: Loads text files into memory with empty line filtering
- File Writing: Saves string arrays to files with proper formatting
- Append Operations: Adds new content to existing files
- Error Resilience: Handles missing files and I/O errors gracefully
5. Performance Monitoring (Timer.java
)
Simple utility for tracking operation execution times:
public static void stop(){
endTime = System.nanoTime();
long executionTimeMs = (endTime - startTime) / 1000000;
long hours = executionTimeMs / 3600000;
long minutes = (executionTimeMs % 3600000) / 60000;
long seconds = (executionTimeMs % 60000) / 1000;
long milliseconds = executionTimeMs % 1000;
System.out.println("Execution time: " + String.format("%02d:%02d:%02d:%03d", hours, minutes, seconds, milliseconds));
}
Data Processing Workflow
Input Format Processing
The application processes two types of input formats:
- Direct URLs:
https://www.youtube.com/watch?v=VIDEO_ID;f=category;q=quality
- Parameter Headers: Lines starting with
f=category;q=quality
that apply to subsequent URLs
Quality Selection Algorithm
The application implements intelligent quality selection:
public String selectBestCode(String link, String resolution){
// Execute youtube-dl -F to get available formats
// Parse output to find best video and audio codes
// Combine video+audio for optimal quality
if(audioCode.equals("") || bestCode.equals("")){
result = "best";
} else {
result = bestCode + "+" + audioCode;
}
return result;
}
Selection Logic:
- Format Discovery: Uses youtube-dl to list available formats
- Quality Matching: Finds exact resolution match or best available
- Audio Combination: Pairs video streams with high-quality audio
- Fallback Strategy: Uses "best" format if specific combination unavailable
Download History Management
The application maintains a persistent history to avoid re-downloading:
- History File:
ids.txt
stores previously downloaded video URLs - Duplicate Detection: Checks URLs against history before downloading
- Configurable Behavior: Can be disabled via
downloadOnlyNewVideos
setting
External Dependencies
youtube-dl Integration
The application relies on the external youtube-dl tool:
- Format Selection: Uses
-F
flag to discover available formats - Download Execution: Constructs complex command-line arguments
- Progress Monitoring: Parses output for download progress and completion status
ffmpeg Integration
Automatically used by youtube-dl for:
- Format Conversion: Merging separate video and audio streams
- Quality Processing: Handling different codecs and containers
Error Handling and Resilience
Robust Command Execution
public boolean runCmd(String cmd){
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
// Process output and track completion...
} catch (Exception err) {
err.printStackTrace();
return false;
}
return percentage.equals("100.0");
}
Error Recovery Features:
- Process Monitoring: Tracks external command execution
- Output Parsing: Extracts progress and error information
- Graceful Degradation: Continues processing other videos if one fails
- User Feedback: Provides clear error messages and status updates
Configuration Files
Default Configuration (config.properties
)
destinationFolderName=C:\\workspace\\stream\\youtube\\
downloadOnlyNewVideos=true
downloadedFileName=%(channel)s-%(upload_date)s-%(title)s-%(id)s-%(resolution)s.%(ext)s
defaultListFile=list.txt
Batch Execution (run.bat
)
Simple Windows batch file for easy execution:
java -jar YoutubeDownloader.jar
pause
This architecture provides a robust, configurable, and user-friendly system for batch downloading YouTube videos with automatic organization and quality control.