FFmpeg 在解码一个小的 mp3 文件时卡住。在处理大约 8 或 9 个数据缓冲区后,崩溃总是发生。我不知道发生了什么。
await e.Channel.SendMessage("Playing");
var server = e.Server;
var voiceChannel = client.FindServers(server.Name).FirstOrDefault().VoiceChannels.FirstOrDefault();
var _vClient = await client.GetService<AudioService>()
.Join(voiceChannel);
var pathOrUrl = "a.mp3";
var process = Process.Start(new ProcessStartInfo
{ // FFmpeg requires us to spawn a process and hook into its stdout, so we will create a Process
FileName = "ffmpeg",
Arguments = $"-i {pathOrUrl} " + // Here we provide a list of arguments to feed into FFmpeg. -i means the location of the file/URL it will read from
"-f s16le -ar 48000 -ac 1 pipe:1", // Next, we tell it to output 16-bit 48000Hz PCM, over 2 channels, to stdout.
UseShellExecute = false,
RedirectStandardOutput = true // Capture the stdout of the process
});
Thread.Sleep(2000); // Sleep for a few seconds to FFmpeg can start processing data.
int blockSize = 3840; // The size of bytes to read per frame; 1920 for mono
byte[] buffer = new byte[blockSize];
int byteCount;
while (true) // Loop forever, so data will always be read
{
byteCount = process.StandardOutput.BaseStream // Access the underlying MemoryStream from the stdout of FFmpeg
.Read(buffer, 0, blockSize); // Read stdout into the buffer
if (byteCount == 0) { // FFmpeg did not output anything
Console.WriteLine("Finished");
break;
}// Break out of the while(true) loop, since there was nothing to read.
_vClient.Send(buffer, 0, byteCount); // Send our data to Discord
}
_vClient.Wait();
最佳答案
你的论点:
-f s16le -ar 48000 -ac 1 pipe:1
这些是我用来转换 mp3 的参数:
-acodec libmp3lame -ar 48000 -ac 2 -map 0:a:0?
我相信
-ac 1
是单声道和 -ac 2
是立体声的https://trac.ffmpeg.org/wiki/AudioChannelManipulation#monostereo
当我将您的参数与
-f s16le
一起使用时转换一个 mp3,文件出来损坏和静音。如果排除它,我认为它仍然会自动转换为 16 位。你也可以试试:
-f pcm_s16le
也许这些细节会引导你解决问题。
关于c# - Ffmpeg 在解码时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42915663/