How to create a .Y4M video file that Chrome can use?

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:

Prerequisites:

  • 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
  • Linux
    • 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

Steps:

  1. Convert the file from .mp4 to .y4m format
  2. 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

Running Chrome:

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
Michael Graves - February 3, 2016

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?

    Tsahi Levent-Levi - February 3, 2016

    Michael,

    Thanks for the suggestion – we’ll check to see if this is possible or not.

    Adam - December 31, 2018

    Yes this works.

    If you convert the video as so:

    ffmpeg filename.mp4 filename.mjpeg

    Then for mac os you can do:

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome –use-file-for-fake-video-capture=.mjpeg –use-fake-device-for-media-stream

Subscribe to the New testRTC Blog • BlogGeek.me - February 4, 2016

[…] Useful tips for testers, like the one we published yesterday about .y4m files and Chrome […]

Automation testing of web application with fake webcam - Saisyam - April 26, 2019

[…] mp4 files small. From my experience, even 2 seconds of mp4 video will give you 40MB of y4m video. This link will explain how to use ffmpeg to convert mp4 to y4m format. If you are an Ubuntu user, you can […]

PacoBell - November 17, 2019

Minor nitpick, but you forgot to add the file output parameter (“YOUR_FILE_HERE.y4m”) to the end of your ffmpeg command line.

    Tsahi Levent-Levi - November 18, 2019

    Thanks!

deng - January 22, 2021

Michael,
Use MJPEG or H.264 will create a decoder in chrome,but *.y4m will not.

Comments are closed