How to adjust mpeg 2 ts start time with ffmpeg?

9.4k views Asked by At

I'm writing simple HLS (Http Live Streaming) java server to live cast (really live, not on demand) screenshow + voice. I constantly get chunks of image frames and audio samples as input to my service and produce mpeg 2 ts files + m3u8 playlist web page as output. The workflow is the following:

  1. Collect (buffer) source video frames and audio for certain period of time
  2. Convert series of video frames to h.264 encoded video file
  3. Convert audio samples to mp3 audio file
  4. Merge them to .ts file with ffmpeg command

    ffmpeg -i audio.mp3 -i video.mp4 -f mpegts -c:a copy -c:v copy -vprofile main -level:v 4.0 -vbsf h264_mp4toannexb -flags -global_header segment.ts
    
  5. Publish several .ts files on m3u8 playlist.

The problem is resulting playlist interrupts after first segment is played. VLC logs following error:

freetype error: Breaking unbreakable line
ts error: libdvbpsi (PSI decoder): TS discontinuity (received 0, expected 4) for PID 17
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 0
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 4096
core error: ES_OUT_SET_(GROUP_)PCR is called too late (pts_delay increased to 1000 ms)
core error: ES_OUT_RESET_PCR called
core error: Could not convert timestamp 185529572000
ts error: libdvbpsi (PSI decoder): TS discontinuity (received 0, expected 4) for PID 17
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 0
ts error: libdvbpsi (PSI decoder): TS duplicate (received 0, expected 1) for PID 4096
core error: ES_OUT_SET_(GROUP_)PCR is called too late (jitter of 8653 ms ignored)
core error: Could not get display date for timestamp 0
core error: Could not convert timestamp 185538017000
core error: Could not convert timestamp 185538267000
core error: Could not convert timestamp 185539295977
...

I guess the reason is that start time of segments do not belong to one stream, but it's impossible to concat and resegment (with ffmepg -f segment) whole stream once new chunk is added. Tried adding #EXT-X-DISCONTINUITY tag to playlist as suggested here but it didn't help. When I ffprobe them I get:

Input #0, mpegts, from '26.ts':
Duration: 00:00:10.02, start: 1.876978, bitrate: 105 kb/s
Program 1
Metadata:
  service_name    : Service01
  service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 640x640, 4 fps, 4 tbr, 90k tbn, 8 tbc
Stream #0:1[0x101]: Audio: mp3 ([3][0][0][0] / 0x0003), 48000 Hz, mono, s16p, 64 kb/s   

Where start value in line Duration: 00:00:10.02, start: 1.876978, bitrate: 105 kb/s is more or less equal for all segments. When I check segments from available proven-to-work playlists (like http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/appleman.m3u8) they all have diffrenet start values for each segment, for example:

Input #0, mpegts, from 'segm150518140104572-424570.ts':
Duration: 00:00:06.17, start: 65884.808689, bitrate: 479 kb/s
Program 257
Stream #0:0[0x20]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 320x180 [SAR 1:1 DAR 16:9], 30 fps, 29.97 tbr, 90k tbn, 60 tbc
Stream #0:1[0x21]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 115 kb/s
Stream #0:2[0x22]: Data: timed_id3 (ID3  / 0x20334449)

and the next after it

Input #0, mpegts, from 'segm150518140104572-424571.ts':
Duration: 00:00:06.22, start: 65890.814689, bitrate: 468 kb/s
Program 257
Stream #0:0[0x20]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 320x180 [SAR 1:1 DAR 16:9], 30 fps, 29.97 tbr, 90k tbn, 60 tbc
Stream #0:1[0x21]: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 124 kb/s
Stream #0:2[0x22]: Data: timed_id3 (ID3  / 0x20334449)

differ in the way that start time of segm150518140104572-424571.ts is equal to start time + duration of segm150518140104572-424570.ts.

How could this start value be adjusted with ffmpeg? Or maybe my whole aproach is wrong? Unfortunately I couldn't find on the internet working example of live (not on demand) video service implemented with ffmepg.

3

There are 3 answers

1
Maxim Kornienko On BEST ANSWER

I've answered my own question with approach described here. Timestamp could be adjusted with -initial_offset parameter of -f segment format:

ffmpeg -i in.ts -vcodec copy -acodec copy -f segment -initial_offset 10 -segment_format mpegts out%d.ts

For each new segment I calculate necessary -initial_offset as sum of all previous segments length.

0
szatmary On

Its more than just timestamps, It is also continuity indicators. So setting the start time will not fix your problem. You must encode as a single stream.

0
Army On

you can set "-segment_time 20" to make ouput only one ts file

fmpeg.exe -i 0.mp3 -c copy -bsf:v aac_adtstoasc -f segment -segment_time 20 -initial_offset 10 -segment_format mpegts out%d.ts