Reputation: 21
environment: Rider 2022.1.2, .NET 6.0, Windows 10
The following piece of code should copy the musicFile that is given and add a cover to the copy, then place it as name: title - artist.extension
in the same directory, for example never gonna give you up - rick astley.mp3
When debugging, if i copy-paste what is saved in the variable p.StartInfo.Arguments
in a CMD terminal, it works perfectly, but in my c# code the program freezes up at p.WaitForExit();
.
What could i be doing wrong?
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "CMD.exe";
p.StartInfo.Arguments = "ffmpeg -i \""+ musicFile +"\" -i \""+ albumInfo.Image.Uri.ToString() +"\" -map 0:a -map 1 -codec copy -metadata:s:v title=\"Album cover\" -metadata:s:v comment=\"Cover (front)\" -disposition:v attached_pic \"" + directoryFile + "\\" + title + " - " + artist + "." + formatName + "\"";
p.Start();
p.WaitForExit();
}
Upvotes: 0
Views: 271
Reputation: 156
You are not exiting after running the command. AFAIK, there is no exit flag in ffmpeg
so I would make use of standard commands here.
Try doing something like this:
p.StartInfo.Arguments = "ffmpeg -i \""+ musicFile +"\" -i \""+ albumInfo.Image.Uri.ToString() +"\" -map 0:a -map 1 -codec copy -metadata:s:v title=\"Album cover\" -metadata:s:v comment=\"Cover (front)\" -disposition:v attached_pic \"" + directoryFile + "\\" + title + " - " + artist + "." + formatName + "\" & exit /b";
This would exit the command prompt as soon as ffmpeg
is done. You might want to think about capturing the output of command before exit.
An alternative could also be to run the command directly without using command prompt since Process class can run the executables. You can get the path of ffmpeg
executable and use that directly. Like this:
Process.Start("[full path of ffmpeg.exe here]", "-i \""+ musicFile +"\" -i \""+ albumInfo.Image.Uri.ToString() +"\" -map 0:a -map 1 -codec copy -metadata:s:v title=\"Album cover\" -metadata:s:v comment=\"Cover (front)\" -disposition:v attached_pic \"" + directoryFile + "\\" + title + " - " + artist + "." + formatName + "\"";
Upvotes: 1