c# - Ffmpeg 在解码时卡住

标签 c# ffmpeg

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/

相关文章:

c# - 定时器启动时调用 Tick 事件

c# - 当参数之一是指针数组时如何从 C# 调用 C++ DLL

javascript - 视频到音频文件转换并通过 node js 中的 FFMPEG 保存

python - 为 Jupyter Notebook 安装 ffmpeg

c# - 在 Treeview 中包裹 TreeviewItem 的 TextBlock 内容

c# - 使用 socket.io 和 C# 客户端与 Compute Engine 上的 Node.js 服务器代码通信

c# - 该字符串的编译错误

ios - FFMPEG 创建模糊视频

flash - 将 RGB 转换为 YUV,+ ffmpeg

python - 是否可以在 aws lambda 函数中安装 s3fs-fuse?