If you’ve ever tried to replace the camera feed for Chrome with a media file for the purpose of testing WebRTC, you might have noticed that it isn’t the most easy process of all.
Even if you got to the point of having a .Y4M file (the format used by Chrome) and finding which command line flags to run Chrome with – there is no guarantee for this to work.
Why? Chrome likes its .Y4M file in a very very specific way. One which FFmpeg fails to support.
We’ve recently refreshed the media files we use ourselves in testRTC – making them a bit more relevant in terms of content type and containing media that can hold high bitrates nicely. Since we have our own scars from this process, we wanted to share it here with you – to save you time.
Why is it so damn hard?
When you use FFmpeg to create a .Y4M file, it generates it correctly. If you try to open the .Y4M file with a VLC player, for example, it renders well. But if you try to give it to Chrome – Chrome will crash from this .Y4M file.
The reason stems in Chrome’s expectations. The header of the .Y4M file includes some metadata. Part of it is the file type. What FFmpeg has is C420mpeg2 to describe the color space used (or something of the sorts), but Chrome expects to see only C420.
Yes. 5 characters making all the trouble.
The only place we found that details it is here, but it still leaves you with a lot to do to get it done.
Here’s what you need to do:
- The media file
- We’ve used .mp4 but .webm is just as good
- Make sure the origin resolution and frame rate is what you need – for the camera source – Chrome will scale down if and when needed
- The source file should be of high quality – again, assume it comes out directly from a camera
- You can make do with Windows or Mac – I’ve used Linux for this though
- I find it easier to handle for command line tasks (and I am definitely not a Mac person)
- FFmpeg installed somewhere (get it from here, but hopefully you have it install already)
- sed command line program
- Convert the file from .mp4 to .y4m format
- Remove the 5 redundant characters
Here’s how to do it from the command line. for all of the .y4m files in your current folder:
ffmpeg -i YOUR-FILE-HERE.mp4 -pix_fmt yuv420p sed -i '0,/C420mpeg2/s//C420/' *.y4m
To run Chrome with your newly created file, you’ll need to invoke it from command line with a few flags:
google-chrome --use-fake-device-for-media-stream --use-file-for-fake-video-capture=YOUR-FILE-HERE.y4m
Things to remember:
- The resulting .Y4M file is humongous. 10 seconds of 1080p resolution eats up a full gigabyte of disk space
- That sed command? It isn’t optimized for speed but it gets the job done, so who cares? It’s a one-time effort anyways
Tsahi – Based upon the numbers you offer I expect that the file is so very large because it’s uncompressed. It may be encoded in 4:2:0 fashion, but that’s just the sampling structure. According to http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 the format is merely a container for uncompressed frames.
We know that Chrome handles webcams that support UVC 1.1 and UVC 1.5, which support on-camera use of MJPEG or H.264 compression. In fact, it’s necessary to use one of these to pass 1080p30 over a USB 2.0 connection.
Perhaps it’s possible to create a file that is also MJPEG encoded? That would achieve a 20:1 reduction in file size on disk. Or perhaps that’s just wishful thinking?