Search code examples
iphoneffmpeghttp-live-streaminglibx264

libx264 fails on HTTP Live Streaming (FFmpeg)


I want to live stream with HTTP Live Streaming, but I have problem with libx264 or maybe something other.

My hardware and software environment:

  • Macbook Pro
  • VirtualBox with Ubuntu 16.04
  • Nginx and FFmpeg (in Ubuntu)

I am able to stream a static video file (in Ubuntu with Nginx and FFmpeg). The FFmpeg command is as following:

$ffmpeg -i /my/sample/video.mp4 -codec:v libx264 -f hls /output/file.m3u8

But when it comes to webcam (live streaming), Mac OS Safari fails to open the stream. I used the following command:

$ffmpeg -i /dev/video0 -codec:v libx264 -f hls /output/file.m3u8

I guess it's the problem of libx264 because when I use mpeg2video encoder, Mac OS Safari indeed can play the stream:

$ffmpeg -i /dev/video0 -codec:v mpeg2video -f hls /output/file.m3u8

I know there is a library called video4linux2, should I use video4linux2 for capturing my webcam? But I don't know the appropriate FFmpeg command for HTTP Live Streaming (I tried FFserver but there is error something like cannot rename hls)

anyone shed some light on my problem?


Solution

  • I have figured it out!

    The reason why Mac OS Safari cannot open a HTTP Live Streaming encoded by libx264 is because the default codec in Mac OS does not support the default output by libx264!

    Simply add -pix_fmt yuv420p to the FFmpeg command and everything works fine:

    $ffmpeg -f video4linux2 -i /your/webcam/path -codec:v libx264 -pix_fmt yuv420p /output/file.m3u8