2

I am trying to implement HLS using FFmpeg for transcoding + segmenting but have been facing a couple of issues that have been bugging me for the past week.

Issue

Webserver currently receives live MP4 fragments being recorded on-the-go and needs to take care of transcoding and segmentation.

As mp4 fragments are being received, they need to be encoded. Then segmented. If i run a segmenter (be it ffmpeg or apple mediastreamsegmenter), every mp4 fragment is being treated as a VOD by itself and I'm not being able to integrate them as part of a larger live event implementation.

I thought of a solution where every time I receive an mp4 fragment, I first use fmpeg to concatenate it with previous ones to form the larger mp4 that I then pass to be segmented for HLS. That did not work either because the entire stream has to be re-segmented each and every time and existing TS fragments replaced by new ones that are similar yet shifted in time.

Implementation 1

ffmpeg -re -i fragmentX.mp4 -b:v 118k -b:a 32k -vcodec copy -preset:v veryfast -acodec aac -strict -2 -ac 2 -f mpegts -y fragmentX.ts

I manage the m3u8 manifest on my own, deleting old fragments and appending new ones.

When validating the stream, I find it stacked with EXT-X-DISCONTINUITY tags making the stream unwatchable.

Implementation 2

First combine latest fragment with overall.mp4

ffmpeg -i "concat:newfragment.mp4|existing.mp4" -c copy overall.mp4

Then pass the combination to ffmpeg for HLS segmentation

ffmpeg -re -i overall.mp4 -ac 2 -r 20 -vcodec libx264 -b:v 318k -preset:v veryfast -acodec aac -strict -2 -b:a 32k -hls_time 2 -hls_list_size 3 -hls_allow_cache 0 -hls_base_url /Users/JosephKalash/Desktop/test/350/ -hls_segment_filename '350/fragment%03d.ts' -hls_flags delete_segments 350/index.m3u8

Concatenation is not perfect and there are noticeable glitches where the fragments are supposed to be stitched. Segmentation replaces older fragments and the manifest is rewritten as if it's a new HLS stream every time ffmpeg is called.

I cannot figure out how to get this to work properly.

Any ideas?

Joe
  • 2,386
  • 1
  • 22
  • 33
  • To avoid the glitches, you need to encode audio and video as a single continuous stream. If your input mp4 is in the correct format and was encoded as a single stream, you can use `-c:a copy -c:v copy` to avoid transcoding. If the input mp4 is not in the correcto format, or if those fragments were encoded separate, you cannot (fully) avoid the glitches. – wimh Aug 22 '15 at 21:20
  • @Wimmel Could you further explain what do you mean by 'single continuous stream' ? I also tried keeping the same audio & video encoders but saw no reduction in glitch. – Joe Aug 23 '15 at 08:16
  • I mean you only use one encoder, which you never restart. That also means you cannot write the output directly to a .ts file, because that file would keep growing unlimited. You can also consider to look at https://github.com/arut/nginx-rtmp-module – wimh Aug 23 '15 at 08:24

1 Answers1

2

Solved by relying on nginx rtmp module which turned out to be suited for the above implementation.

Joe
  • 2,386
  • 1
  • 22
  • 33
  • I wanted to stream my desktop screen using HLS & see how streaming work. I followed the this [www.vultr.com](https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video). Issue here is , how to get key for local server setup. what could be the url for 192.168.1.102 [Streaming your desktop](http://trac.ffmpeg.org/wiki/EncodingForStreamingSites). I don't want to use [OBS project](https://obsproject.com/) – dinu0101 Oct 20 '18 at 22:09