FFMpegCore: (StreamPipeSink) Pipe is broken

Hello,

I am experiencing an issue where I cannot get the StreamPipeSink to work.

Stream input = File.OpenRead("video.ts");  // Will get it from an online source
StreamPipeSource pipeSource = new StreamPipeSource(input);

var output = new MemoryStream();
StreamPipeSink pipeSink = new StreamPipeSink(output);

//Exctract audio to a MemoryStream --> byte[ ]
FFMpegArguments
    .FromPipeInput(pipeSource)
    .OutputToPipe(pipeSink, options => options.WithCustomArgument("-acodec pcm_s16le -osr 16000 -ocl mono"))
    .ProcessSynchronously();

This results in an error:

 Pipe is broken.
   at FFMpegCore.Arguments.InputPipeArgument.ProcessDataAsync(CancellationToken token) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\Arguments\InputPipeArgument.cs:line 27
   at FFMpegCore.Arguments.PipeArgument.During(CancellationToken cancellationToken) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\Arguments\PipeArgument.cs:line 41
   at FFMpegCore.FFMpegArguments.During(CancellationToken cancellationToken) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\FFMpegArguments.cs:line 73
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.WaitAll(Task[] tasks)
   at FFMpegCore.FFMpegArgumentProcessor.ProcessSynchronously(Boolean throwOnError) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\FFMpegArgumentProcessor.cs:line 64
   --- End of inner exception stack trace ---
   at FFMpegCore.FFMpegArgumentProcessor.HandleException(Boolean throwOnError, Exception e, IReadOnlyList`1 errorData) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\FFMpegArgumentProcessor.cs:line 150
   at FFMpegCore.FFMpegArgumentProcessor.ProcessSynchronously(Boolean throwOnError) in C:\dev\FFMpegCore\FFMpegCore\FFMpeg\FFMpegArgumentProcessor.cs:line 73

My own throughts: I have been digging around a bit. The FFMpegArgumentProcessor.ProcessSynchronously(...) calls the cancellationTokenSource.Cancel() before the OutputPipeArgument.ProcessDataAsync(...) is finished, means no data have been read.

 try
            {
                _ffMpegArguments.Pre();
                await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
                {
                    errorCode = t.Result;
                    cancellationTokenSource.Cancel();
                    _ffMpegArguments.Post();
                }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
            }

And it the PipeArgument.Post() is called before the OutputPipeArgument.ProcessDataAsync(...) is finished too (due to the async nature of async). Even through cancellationTokenSource.Cancel(); have been called

Due to PipeArgument.Post() does:

   public void Post()
        {
            Pipe?.Dispose();
            Pipe = null!;
        }

Means the pipe is null.

The last thing PipeArgument.During(...) does is calling:

 Pipe.Disconnect();

But at this point the pipe is already null, hence the error.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 28 (11 by maintainers)

Most upvoted comments

I get this on FFMpegException#FFMpegErrorOutput

ffmpeg version 4.2.4-1ubuntu0.1 Copyright © 2000-2020 the FFmpeg developers built with gcc 9 (Ubuntu 9.3.0-10ubuntu2) configuration: --prefix=/usr --extra-version=1ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared libavutil 56. 31.100 / 56. 31.100 libavcodec 58. 54.100 / 58. 54.100 libavformat 58. 29.100 / 58. 29.100 libavdevice 58. 8.100 / 58. 8.100 libavfilter 7. 57.100 / 7. 57.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 [mp3 @ 0x556b07c20740] invalid concatenated file detected - using bitrate for duration Input #0, mp3, from ‘unix:/tmp/CoreFxPipe_FFMpegCore_89ee8bae-82dc-43ab-b6d0-0dac7aed5827’: Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf55.15.100 Duration: N/A, start: 0.023021, bitrate: 128 kb/s Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s [NULL @ 0x556b07c25b40] Unable to find a suitable output format for ‘unix:/tmp/CoreFxPipe_FFMpegCore_60e48937-baa1-4b7f-9424-96c2a6f5e00a’ unix:/tmp/CoreFxPipe_FFMpegCore_60e48937-baa1-4b7f-9424-96c2a6f5e00a: Invalid argument

To fix this error, you need to add .ForceFormat(“wav”), which gives ffmpeg -f wav. ffmpeg normally uses the extension of the output file path to determine the format. When outputting to a stream, that is not possible and that will almost always require it to be specified.

FFMpegCore does not use StandardInput.BaseStream to “pass along” the input stream. Instead it uses NamedPipeServerStream, which btw uses unix sockets on non-Windows machines

@renanrcp could you try using version 3.2.2 or newer?

@renanrcp I was able to fix PipeBroken1Async and PipeBroken2Async just by adding .ForceFormat(“opus”) and .ForceForce(“mp3”). I’m looking into PipeBroken3Async since that one gives a null ref exception after adding ForceFormat(“raw”) to input and ForceFormat(“opus”) to output

A temporary solution is use -f instead -acoded

Example:

            var processor = FFMpegArguments
            .FromPipeInput(new StreamPipeSource(file))
            .OutputToPipe(new StreamPipeSink(outputStream), options =>
            {
                options.WithAudioCodec("libopus");
            });

to

            var processor = FFMpegArguments
            .FromPipeInput(new StreamPipeSource(file))
            .OutputToPipe(new StreamPipeSink(outputStream), options =>
            {
                options.WithCustomArgument("-f opus");
            });

I do this repo with 3 broken pipes example and one working (just decoding, if you try encoding will broke)