J.Clarke
J.Clarke

Reputation: 370

Ffmpeg freezes while decoding

FFmpeg freezes while decoding a small mp3 file. Thecrash happens conistantly after proscessing about 8 or 9 data buffers. I have no clue as to what is happening.

           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();

Upvotes: 0

Views: 785

Answers (2)

J.Clarke
J.Clarke

Reputation: 370

I was missing a dependency on the discord side that was making it appear as if ffmpeg was freezing.

Upvotes: 0

Matt McManis
Matt McManis

Reputation: 4675

Your arguments:

-f s16le -ar 48000 -ac 1 pipe:1

These are the arguments I use to convert mp3:

-acodec libmp3lame -ar 48000 -ac 2 -map 0:a:0?

I believe -ac 1 is mono and -ac 2 is stereo
https://trac.ffmpeg.org/wiki/AudioChannelManipulation#monostereo

When I used your args with -f s16le to convert an mp3, the file came out corrupt and mute. If you exclude it, I think it still converts to 16-bit automatically.

You can also try:

-f pcm_s16le

Maybe these details will lead help you solve the problem.

Upvotes: 1

Related Questions