I don’t know why Ffmpeg works but it gives an error. How can you see in this code my ffmpeg is started and begins to merge the media file. Media file his merge but he gives an error I don’t know why it happens.
The error happens in this line code
He caused “if” and my method returned false. But it still works he’s merage my audio and video. I give your my all code
public static async Task<bool> VideoHighQuality(YoutubeClient youtube, string url)
{
try
{
var video = await youtube.Videos.GetAsync(url);
var title = video.Title;
var safeTitle = string.Join("_", title.Split(Path.GetInvalidFileNameChars()));
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(url);
var videoStreamInfo = streamManifest.GetVideoOnlyStreams()
.Where(s => s.VideoQuality.Label.StartsWith("1080"))
.FirstOrDefault() ?? streamManifest.GetVideoOnlyStreams()
.OrderByDescending(s => s.VideoQuality)
.FirstOrDefault();
var audioStreamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
if (videoStreamInfo != null && audioStreamInfo != null)
{
var outputPath = Path.Combine("E:\films", $"{safeTitle}_merged.{videoStreamInfo.Container}");
var videoTempPath = Path.Combine("E:\films", $"video_{safeTitle}.{videoStreamInfo.Container}");
var audioTempPath = Path.Combine("E:\films", $"audio_{safeTitle}.{audioStreamInfo.Container}");
await youtube.Videos.Streams.DownloadAsync(videoStreamInfo, videoTempPath);
await youtube.Videos.Streams.DownloadAsync(audioStreamInfo, audioTempPath);
var mergeSuccess = await MergeVideo(videoTempPath, audioTempPath, outputPath);
File.Delete(videoTempPath);
File.Delete(audioTempPath);
Console.WriteLine($"Video successfully processed and saved to: {outputPath}");
return mergeSuccess;
}
else
{
Console.WriteLine("Suitable video or audio stream not found.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
return false;
}
}
private static async Task<bool> MergeVideo(string videoPath, string audioPath, string outputPath)
{
try
{
Console.WriteLine("Starting MergeVideo method.");
var ffmpeg = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"C:UsersromapsourcereposVideoDownloaderFfmpegffmpeg.exe",
Arguments = $"-i "{videoPath}" -i "{audioPath}" -c:v copy -c:a aac -strict experimental "{outputPath}"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
ffmpeg.Start();
var outputTask = ffmpeg.StandardOutput.ReadToEndAsync();
var errorTask = ffmpeg.StandardError.ReadToEndAsync();
await ffmpeg.WaitForExitAsync();
string output = await outputTask;
string error = await errorTask;
Console.WriteLine("FFmpeg process completed.");
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine($"FFmpeg Error: {error}");
return false;
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred in MergeVideo: {ex.Message}");
return false;
}
}
I tried many reasons to explore this solution.
Use GPT and Claude but he doesn’t help me. I actually don’t understand how I can solve this ’cause I do this first time
1