title: ffmpeg problems with streaming mp4 over udp in local network
body: <p>I'm streaming mp4 video files (some of them are avi converted to mp4 with ffmpeg earlier) over <code>udp://232.255.23.23:1234</code> from linux (embedded) with ffmpeg v3.4.2 to multiple linux (antix) machines that play the stream with MPV, all of this happens in local network so I expected it to work flawlessly, but unfortunately it doesn't.</p>
  
  <p>Here are the original commands I tried to use:</p>
  
  <p>ffmpeg</p>
  
  <pre><code>ffmpeg -re -i PATH_TO_FILE.mp4 -c copy -f mpegts udp://232.255.23.23:1234
  </code></pre>
  
  <p>mpv</p>
  
  <pre><code>mpv --no-config --geometry=[geo settings] --no-border udp://232.255.23.23:1234
  </code></pre>
  
  <p>This seemed to woork good, however a problem appeared that on the displaying end, the stream is actually much longer than the streamed content itself. The mp4 files in total have ~5 minutes 36 seconds, and mpv plays the entire stream loop in >= 6 minutes. I think it's happening because of dropped frames, that mpv waits to recover or something and therefore extends the length of the actual content. This cannot work in my case, as I have a precise time gap for displaying the stream and it cannot be longer than the streamed content.
  All the content is made in 1680x800 resolution and is displayed on a screen with 1680x1050 resoltion (positioned with mpv geometry)</p>
  
  <p>It appears that using this command for mpv:</p>
  
  <pre><code>mpv --no-config --framedrop=no --geometry=[geo settings] --no-border udp://232.255.23.23:1234
  </code></pre>
  
  <p>made the duration correct, however this introduces huge artifacts in the videos sometimes.</p>
  
  <p>I read that using <code>-re</code> for streaming can cause these frame drops, so I tried putting a static number of fps for both file input and output stream, for example:</p>
  
  <pre><code>ffmpeg -re -i PATH_TO_FILE.mp4 -c copy -r 25 -f mpegts udp://232.255.23.23:1234
  </code></pre>
  
  <p>This reads the file at native framerate and outputs the stream at 25fps, and it appears to have the timing duration correct, but it also causes occasional articats and I think has worse qualit overall. Output from mpv when one of the artifacts happened:</p>
  
  <pre><code>[ffmpeg/video] h264: cabac decode of qscale diff failed at 85 19
  [ffmpeg/video] h264: error while decoding MB 85 19, bytestream 85515
  </code></pre>
  
  <p>I also tried using <code>--untimed</code> or <code>--no-cache</code> in mpv, but this causes stutters in the video </p>
  
  <p>I'm also getting requent <code>Invalid video timestamp</code> warnings in MPV, for example: <code>Invalid video timestamp: 1.208333 -&gt; -8.711667</code></p>
  
  <p>Playing in mpv without <code>--no-config</code> and with <code>--untimed</code> added also causes frequent artifacts:</p>
  
  <pre><code>V: -00:00:00 / 00:00:00 Cache:  0s+266KB
  [ffmpeg/video] h264: Invalid NAL unit 8, skipping.
  V: -00:00:00 / 00:00:00 Cache:  0s+274KB
  [ffmpeg/video] h264: Reference 4 &gt;= 4
  [ffmpeg/video] h264: error while decoding MB 6 0, bytestream 31474
  [ffmpeg/video] h264: error while decoding MB 78 49, bytestream -12
  V: 00:00:06 / 00:00:00 Cache:  5s+11KB
  Invalid video timestamp: 6.288333 -&gt; -8.724933
  V: -00:00:05 / 00:00:00 Cache:  3s+0KB
  [ffmpeg/video] h264: Invalid NAL unit 8, skipping.
  [ffmpeg/video] h264: error while decoding MB 59 24, bytestream -27
  V: -00:00:04 / 00:00:00 Cache:  3s+0KB
  [ffmpeg/video] h264: Reference 4 &gt;= 3
  [ffmpeg/video] h264: error while decoding MB 5 2, bytestream 13402
  V: -00:00:03 / 00:00:00 Cache:  2s+0KB
  [ffmpeg/video] h264: Reference 5 &gt;= 4
  [ffmpeg/video] h264: error while decoding MB 51 21, bytestream 9415
  </code></pre>
  
  <p>I tried playing the stream with ffplay and it also caused the videos to be "played" 20 seconds longer.
  Is there any way to keep the streaming duration intact and prevent those huge artifacts? These aren't any huge video files, they are few MB each, everything happens in local network so the latencies are minimal.</p>
  
  <p>Output from ffmpeg when streaming one of the files:</p>
  
  <pre><code> libavutil      55. 78.100 / 55. 78.100
    libavcodec     57.107.100 / 57.107.100
    libavformat    57. 83.100 / 57. 83.100
    libavdevice    57. 10.100 / 57. 10.100
    libavfilter     6.107.100 /  6.107.100
    libswscale      4.  8.100 /  4.  8.100
    libswresample   2.  9.100 /  2.  9.100
  Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'SDM.mp4':
    Metadata:
      major_brand     : isom
      minor_version   : 512
      compatible_brands: isomiso2avc1mp41
      encoder         : Lavf57.48.100
    Duration: 00:00:20.00, start: 0.000000, bitrate: 1883 kb/s
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1680x800 [SAR 1:1 DAR 21:10], 1880 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
      Metadata:
        handler_name    : VideoHandler
  Output #0, mpegts, to 'udp://232.255.23.23:1234':
    Metadata:
      major_brand     : isom
      minor_version   : 512
      compatible_brands: isomiso2avc1mp41
      encoder         : Lavf57.83.100
      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1680x800 [SAR 1:1 DAR 21:10], q=2-31, 1880 kb/s, 24 fps, 24 tbr, 90k tbn, 25 tbc (default)
      Metadata:
        handler_name    : VideoHandler
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (copy)
  Press [q] to stop, [?] for help
  frame=  480 fps= 24 q=-1.0 Lsize=    5009kB time=00:00:19.87 bitrate=2064.7kbits/s speed=   1x
  video:4592kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 9.082929%
  </code></pre>
  
  <p><strong>Edit:</strong> all the files don't contain any audio, so it should be even less traffic on the network</p>