Search code examples
ffmpeghttp-live-streaming

Loop image ffmpeg HLS


Trying to loop an image to get a segmented HLS output.

ffmpeg -loop 1 -i image.png -vcodec libx264 -acodec aac -map 0 -f segment -segment_time 5 -segment_list /seg.m3u8  /200_%06d.ts

  Metadata:
    encoder         : Lavf54.26.101
    Stream #0:0: Video: h264, yuv444p, 1344x840, q=-1--1, 90k tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (png -> libx264)
Press [q] to stop, [?] for help
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec libx264: Invalid argument
[mpegts @ 0x7fe91a615600] H.264 bitstream malformed, no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)
av_interleaved_write_frame(): Invalid argument

Adding in the filter does not seem to help.


Solution

  • As indicated (but not really explained) in the error message, there's an issue converting the h.264 video data to the MPEG2 transport stream. In the default h.264 container (MPEG4), things are length prefixed, whereas in MPEG2 transport streams, start codes are used. The mapping is described in an Annex of the h.264 spec, the mentioned Annex 2. Docs: http://git.videolan.org/?p=ffmpeg.git;a=blob;f=doc/bitstream_filters.texi

    So to properly create the MPEG2 TS, you need to add -vbsf h264_mp4toannexb to the command line. You might also need to add the following arguments as well -flags -global_header as well. This is to make sure that some of the codec parameters are included in-band (within the transport stream).