A powerful and feature-rich YouTube to MP3 converter library that supports both YouTube and YouTube Music, including playlist functionality, remote conversion, and extensive customization options.
- π΅ Convert YouTube videos to multiple audio formats
- π Full playlist support with customizable filters
- π§ YouTube Music support
- π Real-time progress tracking (File-based or Redis)
- π Remote server conversion support
- π Token-based security
- π― Clean and modern API
- π Automatic file cleanup
- π οΈ Extensive configuration options
- π Asynchronous processing support
composer require darkwob/youtube-mp3-converter
- PHP >= 7.4
- JSON extension
- FFmpeg (optional, for advanced audio processing)
- Redis (optional, for Redis-based progress tracking)
use Darkwob\YoutubeMp3Converter\Converter\YouTubeConverter;
use Darkwob\YoutubeMp3Converter\Progress\FileProgress;
// Initialize progress tracker
$progress = new FileProgress(__DIR__ . '/progress');
// Initialize converter
$converter = new YouTubeConverter(
__DIR__ . '/bin', // Binary path (yt-dlp, ffmpeg)
__DIR__ . '/downloads', // Output directory
__DIR__ . '/temp', // Temporary directory
$progress // Progress tracker
);
// Convert a video
try {
$result = $converter->processVideo('https://www.youtube.com/watch?v=VIDEO_ID');
if ($result['success']) {
foreach ($result['results'] as $video) {
echo "Converted: {$video['title']}\n";
echo "File: {$video['file']}\n";
}
}
} catch (ConverterException $e) {
echo "Error: " . $e->getMessage();
}
use Darkwob\YoutubeMp3Converter\Converter\Options\ConverterOptions;
$options = new ConverterOptions();
$options
->setAudioFormat('mp3') // mp3, wav, aac, m4a, opus, vorbis, flac
->setAudioQuality(0) // 0 (best) to 9 (worst)
->setVideoFormat('bestaudio/best') // Video format selection
->enableSponsorBlock() // Skip sponsored segments
->setPlaylistItems('1-10') // Process specific items
->setDateFilter('20220101', '20231231') // Date range filter
->setFileSizeLimit('100M') // Maximum file size
->setOutputTemplate('%(title)s.%(ext)s') // Custom output template
->setProxy('socks5://127.0.0.1:1080') // Proxy configuration
->setRateLimit(3) // Downloads per minute
->enableThumbnail() // Embed thumbnail
->setMetadata([ // Custom metadata
'artist' => '%(uploader)s',
'title' => '%(title)s'
]);
$converter = new YouTubeConverter($binPath, $outputDir, $tempDir, $progress, $options);
use Darkwob\YoutubeMp3Converter\Converter\Remote\RemoteConverter;
$remote = new RemoteConverter(
'https://api.converter.com',
'your-api-token'
);
// Async conversion
$jobId = $remote->startConversion($url, $options);
// Check progress
$status = $remote->getProgress($jobId);
// Download when ready
if ($status['status'] === 'completed') {
$remote->downloadFile($jobId, 'output.mp3');
}
use Darkwob\YoutubeMp3Converter\Progress\RedisProgress;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$progress = new RedisProgress($redis, 'converter:', 3600);
// Track progress
$progress->update('video123', 'downloading', 50, 'Downloading video...');
// Get progress
$status = $progress->get('video123');
echo "Progress: {$status['progress']}%\n";
echo "Status: {$status['status']}\n";
echo "Message: {$status['message']}\n";
Main class for video conversion operations.
processVideo(string $url): array
- Process a single video or playlistgetVideoInfo(string $url): array
- Get video metadatadownloadVideo(string $url, string $id): string
- Download video file
Configuration options for the converter.
setAudioFormat(string $format): self
- Set output audio formatsetAudioQuality(int $quality): self
- Set audio quality (0-9)setVideoFormat(string $format): self
- Set video format selectionenableSponsorBlock(): self
- Enable SponsorBlock integrationsetPlaylistItems(string $items): self
- Set playlist items to processsetDateFilter(string $start, string $end): self
- Set date range filtersetFileSizeLimit(string $limit): self
- Set maximum file sizesetOutputTemplate(string $template): self
- Set output filename templatesetProxy(string $proxy): self
- Set proxy serversetRateLimit(int $limit): self
- Set rate limitenableThumbnail(): self
- Enable thumbnail embeddingsetMetadata(array $metadata): self
- Set audio metadata
Handle remote conversion operations.
startConversion(string $url, ConverterOptions $options): string
- Start remote conversiongetProgress(string $jobId): array
- Get conversion progressdownloadFile(string $jobId, string $output): bool
- Download converted file
Both FileProgress
and RedisProgress
implement ProgressInterface
:
update(string $id, string $status, float $progress, string $message): void
get(string $id): ?array
delete(string $id): void
getAll(): array
cleanup(int $maxAge = 3600): void
The package uses custom exceptions for different error scenarios:
use Darkwob\YoutubeMp3Converter\Converter\Exceptions\ConverterException;
try {
$result = $converter->processVideo($url);
} catch (ConverterException $e) {
switch (true) {
case $e instanceof ConverterException:
// Handle conversion errors
break;
// Handle other specific exceptions
}
}
- Token-based authentication for remote conversion
- Rate limiting support
- Proxy support for restricted networks
- Input validation and sanitization
- Secure file handling
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For more detailed documentation and examples, visit our Wiki.
This package is for educational purposes only. Please respect YouTube's terms of service and copyright laws when using this package.