Real YouTube download & MP3 conversion with yt-dlp
- Integrate yt-dlp.exe for real YouTube audio download/conversion - Serve actual MP3 files from wwwroot/downloads via API and static files - Report real-time progress from yt-dlp to frontend - Enhance DownloadController: file serving, metadata, deletion endpoints - Update VideoController to track and return real file info/URLs - Enable static file serving in Program.cs; update API info/logging - Update Blazor frontend to use real download URLs and remove old JS logic - Add README_UPDATED.md with new workflow and troubleshooting - Improve error handling, logging, and file cleanup throughout backend - Update .gitignore for downloads and yt-dlp.exe
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Text.RegularExpressions;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace TuneRipper.Api.Services;
|
||||
|
||||
@@ -11,17 +13,20 @@ public interface IVideoService
|
||||
Task<YouTubeVideoDetails?> GetVideoDetailsAsync(string url);
|
||||
bool IsValidYouTubeUrl(string url);
|
||||
string ExtractVideoId(string url);
|
||||
Task<string?> ProcessVideoWithProgressAsync(string url, string format, Action<int, string> progressCallback);
|
||||
}
|
||||
|
||||
public class VideoService : IVideoService
|
||||
{
|
||||
private readonly ILogger<VideoService> _logger;
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
|
||||
public VideoService(ILogger<VideoService> logger)
|
||||
public VideoService(ILogger<VideoService> logger, IWebHostEnvironment environment, IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient = httpClientFactory.CreateClient();
|
||||
_environment = environment;
|
||||
}
|
||||
|
||||
public async Task<YouTubeVideoDetails?> GetVideoDetailsAsync(string url)
|
||||
@@ -85,6 +90,169 @@ public class VideoService : IVideoService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> ProcessVideoWithProgressAsync(string url, string format, Action<int, string> progressCallback)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ytDlpPath = Path.Combine(_environment.WebRootPath, "yt-dlp.exe");
|
||||
|
||||
if (!File.Exists(ytDlpPath))
|
||||
{
|
||||
throw new FileNotFoundException($"yt-dlp.exe not found at {ytDlpPath}");
|
||||
}
|
||||
|
||||
// Create downloads directory if it doesn't exist
|
||||
var downloadDir = Path.Combine(_environment.WebRootPath, "downloads");
|
||||
Directory.CreateDirectory(downloadDir);
|
||||
|
||||
// Generate safe filename
|
||||
var videoId = ExtractVideoId(url);
|
||||
var fileName = $"{videoId}_{DateTime.Now:yyyyMMdd_HHmmss}.%(ext)s";
|
||||
var outputTemplate = Path.Combine(downloadDir, fileName);
|
||||
|
||||
// Build yt-dlp arguments
|
||||
var arguments = new StringBuilder();
|
||||
arguments.Append($"--extract-audio ");
|
||||
arguments.Append($"--audio-format {format} ");
|
||||
arguments.Append($"--audio-quality 0 ");
|
||||
arguments.Append("--no-playlist ");
|
||||
arguments.Append("--no-warnings ");
|
||||
arguments.Append("--newline ");
|
||||
arguments.Append($"--output \"{outputTemplate}\" ");
|
||||
arguments.Append($"\"{url}\"");
|
||||
|
||||
_logger.LogInformation("Starting yt-dlp process with arguments: {Args}", arguments.ToString());
|
||||
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = ytDlpPath,
|
||||
Arguments = arguments.ToString(),
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = downloadDir
|
||||
};
|
||||
|
||||
using var process = Process.Start(processInfo);
|
||||
if (process == null)
|
||||
{
|
||||
throw new Exception("Failed to start yt-dlp process");
|
||||
}
|
||||
|
||||
var outputBuilder = new StringBuilder();
|
||||
var errorBuilder = new StringBuilder();
|
||||
|
||||
// Monitor progress
|
||||
process.OutputDataReceived += (sender, e) =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(e.Data))
|
||||
{
|
||||
outputBuilder.AppendLine(e.Data);
|
||||
_logger.LogDebug("yt-dlp output: {Output}", e.Data);
|
||||
|
||||
// Parse progress from yt-dlp output
|
||||
ParseProgressFromOutput(e.Data, progressCallback);
|
||||
}
|
||||
};
|
||||
|
||||
process.ErrorDataReceived += (sender, e) =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(e.Data))
|
||||
{
|
||||
errorBuilder.AppendLine(e.Data);
|
||||
_logger.LogWarning("yt-dlp error: {Error}", e.Data);
|
||||
}
|
||||
};
|
||||
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
|
||||
// Wait for process completion with timeout
|
||||
var completed = await Task.Run(() => process.WaitForExit(300000)); // 5 minute timeout
|
||||
|
||||
if (!completed)
|
||||
{
|
||||
process.Kill();
|
||||
throw new TimeoutException("yt-dlp process timed out after 5 minutes");
|
||||
}
|
||||
|
||||
if (process.ExitCode != 0)
|
||||
{
|
||||
var error = errorBuilder.ToString();
|
||||
throw new Exception($"yt-dlp failed with exit code {process.ExitCode}. Error: {error}");
|
||||
}
|
||||
|
||||
// Find the downloaded file
|
||||
var downloadedFiles = Directory.GetFiles(downloadDir, $"{videoId}_*.*")
|
||||
.Where(f => !f.EndsWith(".part"))
|
||||
.OrderByDescending(File.GetCreationTime)
|
||||
.ToArray();
|
||||
|
||||
if (downloadedFiles.Length == 0)
|
||||
{
|
||||
throw new Exception("No downloaded file found after processing");
|
||||
}
|
||||
|
||||
var downloadedFile = downloadedFiles[0];
|
||||
var finalFileName = Path.GetFileName(downloadedFile);
|
||||
|
||||
_logger.LogInformation("Successfully processed video. File: {FileName}", finalFileName);
|
||||
|
||||
progressCallback(100, "Processing completed successfully");
|
||||
|
||||
return finalFileName;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error processing video with yt-dlp: {Url}", url);
|
||||
progressCallback(0, $"Processing failed: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseProgressFromOutput(string output, Action<int, string> progressCallback)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Parse different yt-dlp progress patterns
|
||||
if (output.Contains("[download]"))
|
||||
{
|
||||
// Example: "[download] 45.2% of 3.45MiB at 1.23MiB/s ETA 00:02"
|
||||
var percentMatch = Regex.Match(output, @"\[download\]\s+(\d+(?:\.\d+)?)%");
|
||||
if (percentMatch.Success)
|
||||
{
|
||||
if (double.TryParse(percentMatch.Groups[1].Value, out var percent))
|
||||
{
|
||||
var progressPercent = (int)Math.Min(percent * 0.7, 70); // Download is 70% of total progress
|
||||
progressCallback(progressPercent, "Downloading audio...");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (output.Contains("100%") || output.Contains("has already been downloaded"))
|
||||
{
|
||||
progressCallback(70, "Download complete, converting...");
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (output.Contains("[ffmpeg]"))
|
||||
{
|
||||
progressCallback(85, "Converting to MP3...");
|
||||
return;
|
||||
}
|
||||
else if (output.Contains("Deleting original file"))
|
||||
{
|
||||
progressCallback(95, "Finalizing...");
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error parsing progress from output: {Output}", output);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsValidYouTubeUrl(string url)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
|
||||
Reference in New Issue
Block a user