title: Puzzled with file descriptor in Bash (ffmpeg video capture)
body: <p>I am trying to use file descriptors in Bash and found a problem I cannot solve.
  I have to read a video stream coming from the standard output of a command executed in a <code>coproc</code>. This piece of code works as expected :</p>
  
  <pre><code>ffmpeg \
      -i &lt;(exec cat &lt;&amp;${COPROC[0]}) \
      -c:v $ENCODE_VIDEO_FORMAT_LOSSLESS $ENCODE_VIDEO_OPTIONS_LOSSLESS \
      -c:a copy \
      -progress /dev/fd/1 \
      "${capfile}"
  </code></pre>
  
  <p>But the <code>cat</code> process is not really useful since <code>ffmpeg -i pipe:&lt;file descriptor&gt;</code> seems to do the same. So I tried the following code which fails with <code>pipe:63: Bad file descriptor
  </code> error.</p>
  
  <pre><code>ffmpeg \
      -i pipe:"${COPROC[0]}" \
      -c:v $ENCODE_VIDEO_FORMAT_LOSSLESS $ENCODE_VIDEO_OPTIONS_LOSSLESS \
      -c:a copy \
      -progress /dev/fd/1 \
      "${capfile}"
  </code></pre>
  
  <p>The actual script is something a bit complicated but here is a minimal testing code for this issue :</p>
  
  <pre><code>#!/bin/bash
  #
  
  ENCODE_VIDEO_FORMAT_LOSSLESS=ffv1
  ENCODE_VIDEO_OPTIONS_LOSSLESS="-level 3 -threads 7 -coder 1 -context 1 -g 1 -slices 30 -slicecrc 1"
  
  capfile=capure.mkv
  
  coproc ffmpeg  -i file:'Camomille.mkv' -c:v copy -c:a copy -f matroska pipe:1
  
  capture_fd=${COPROC[0]}
  echo "hk_capture_pid=${COPROC_PID}"
  
  ffmpeg \
      -i pipe:${COPROC[0]} \
      -c:v $ENCODE_VIDEO_FORMAT_LOSSLESS $ENCODE_VIDEO_OPTIONS_LOSSLESS \
      -c:a copy \
      -progress /dev/fd/1 \
      "${capfile}"
  </code></pre>
  
  <p>This is the output of the second <code>ffmpeg</code> command : </p>
  
  <pre><code>ffmpeg version 4.1.4-1build2 Copyright (c) 2000-2019 the FFmpeg developers
    built with gcc 9 (Ubuntu 9.2.1-4ubuntu1)
    configuration: --prefix=/usr --extra-version=1build2 --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-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
    libavutil      56. 22.100 / 56. 22.100
    libavcodec     58. 35.100 / 58. 35.100
    libavformat    58. 20.100 / 58. 20.100
    libavdevice    58.  5.100 / 58.  5.100
    libavfilter     7. 40.101 /  7. 40.101
    libavresample   4.  0.  0 /  4.  0.  0
    libswscale      5.  3.100 /  5.  3.100
    libswresample   3.  3.100 /  3.  3.100
    libpostproc    55.  3.100 / 55.  3.100
  pipe:63: Bad file descriptor
  av_interleaved_write_frame(): Broken pipe                                                                                       
  Error writing trailer of pipe:1: Broken pipe                                                                                    
  frame=    4 fps=0.0 q=-1.0 Lsize=      48kB time=00:00:00.03 bitrate=10051.1kbits/s speed=3.44x    
  video:86kB audio:1kB subtitle:0kB other streams:0kB global headers:2kB muxing overhead: unknown
  Conversion failed!
  </code></pre>
  
  <p>This one fails and if you replace <code>-i pipe:${COPROC[0]}</code> by <code>-i &lt;(exec cat &lt;&amp;${COPROC[0]})</code> a capture.mkv file is created.</p>
  
  <p>I run ubuntu eoan and bash version is : <code>GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)</code>. I upgraded several times since I started with this issue, so it wouldn't be related too much to bash and ffmpeg versions.</p>
  
  <p>If someone can point me to what I do wrong with bash file descriptors I would be grateful.</p>