[FFmpeg-user] How does ffmpeg stream discovery work?

Виктор Митрофанов crimson273 at gmail.com
Fri Jul 21 11:44:54 EEST 2023


Hi. I am wondering where to find info on how ffmpeg's stream discovery
works?
I need to give you some context so sorry for the long read:
I use some php coding to manipulate hls playlist by replacing some of its
chunks.
1 - I have some incoming live stream via RTMP that gets published on nginx
with rtmp module. I transcode it with ffmpeg and record it on the disk in
hls through nginx with rtmp module. (get transcoded stream published to
nginx thru rtmp and then record it with nginx). So I get hls playlist and
chunks on the storage that gets updated.
2 - I have some pre-transcoded hls playlist and chunks which is stored on
the local storage. The stream settings for both live transcoded and
pre-transcoded streams are identical.
3 - I use php script to parse live hls playlist I am storing in step 1 and
playlist I have already stored in step 2. I use some logic to replace some
chunks in playlist 1 with chunks from playlist 2 and output modified
playlist. (I "switch" contnent within playlist by manipulating chunks)
4 - I use modified playlist to play it in video player and to re-stream it
further through RTMP.

Now this scheme works. The problems start when I switch from nginx-rtmp to
other streaming server (flussonic in my case).

With flussonic I am doing exactly the same, although it does not record in
hls, it stores its dvr in some binary format and I get access to playlist
thru http request to flussonic rather than just reading files from storage.

What happens is that the moment when ffmpeg receive chunks taken from
playlist 2, for some reason its says that this a new video/audio streams.
As these are a new streams, it does not pick it up which effectively means
that it skips all the chunks from playlist 2 and stream freezes until
manipulated playlist switches back to chunks from playlist 1, then encoding
resumes.

The ffmpeg command for using manipulated playlist is as simple as
ffmpeg -re -i - -c copy -f flv <some-destination>
In this example I read from pipe (I use php script to send buffered chunks
to stdout and ffmpeg reads from stdout). But I've also tried to suuply
manipulated playlist as input with the same result.

Here's some parts of the ffmpeg debug log
Input #0, mpegts, from 'pipe:':
  Duration: N/A, start: 27037.551844, bitrate: N/A
  Program 1
  Stream #0:0[0xd3], 127, 1/90000: Video: h264 (High), 1 reference frame
([27][0][0][0] / 0x001B), yuv420p(progressive, left), 640x360 (640x368)
[SAR 1:1 DAR 16:9], 0/1, 25 fps, 25 tbr, 90k tbn, 50 tbc
  Stream #0:1[0xdd], 218, 1/90000: Audio: aac (LC) ([15][0][0][0] /
0x000F), 44100 Hz, stereo, fltp, 130 kb/s
Successfully opened the file.
.....
cur_dts is invalid st:0 (0) [init:1 i_done:0 finish:0] (this is harmless if
it occurs once at the start per stream)
cur_dts is invalid st:1 (0) [init:1 i_done:0 finish:0] (this is harmless if
it occurs once at the start per stream)
Automatically inserted bitstream filter 'aac_adtstoasc'; args=''
cur_dts is invalid st:0 (0) [init:1 i_done:0 finish:0] (this is harmless if
it occurs once at the start per stream)
[mpegts @ 0x5f17d00] Continuity check failed for pid 0 expected 0 got
41.5kbits/s speed=   1x
[mpegts @ 0x5f17d00] stream=2 stream_type=1b pid=100 prog_reg_desc=
[mpegts @ 0x5f17d00] stream=3 stream_type=f pid=101 prog_reg_desc=
[mpegts @ 0x5f17d00] New video stream 0:2 at pos:9325364 and DTS:5.4s
[mpegts @ 0x5f17d00] probing stream 3 pp:2500
[mpegts @ 0x5f17d00] Probe with size=208, packets=1 detected aac with
score=51
[mpegts @ 0x5f17d00] probed stream 3
[mpegts @ 0x5f17d00] New audio stream 0:3 at pos:9366724 and DTS:5.4042s
[mpegts @ 0x5f17d00] Continuity check failed for pid 0 expected 4 got 0
timestamp discontinuity for stream #0:1 (id=221, type=audio): 15998358, new
offset= -27053550202

Now streams 2 and 3 appear in exact moment ffmpeg receives chunks from
playlist 2.

I am wondering how to solve this issue and change this behaviour. As I
don't really now where to start, I decided to start with figuring out how
ffmpeg determines, that "this is a new stream". Probably with this
knowledge I will find a way to stop him from doing this.

The other interesting moment that "video player" (just some web player on
js, similar to youtube player) handles chunks from playlist 2 well, it does
show me these chunks. VLC player also handles this well. ffmpeg and ffplay
do not.

Any help is strongly appreciated.

Best regards, Viktor Mitrofanov


More information about the ffmpeg-user mailing list