Enabling Proprietary Audio/Video Codecs in NW.js
Due to licensing issues, NW.js cannot play files encoded with proprietary formats (such as H.264 and AAC) out of the box. However, there is a quick and easy workaround to that problem. This article will tell you what it is.
A bit of background
[TLDR: If you just want the solution, just skip ahead to the step by step recipe.]
NW.js is based on Chromium. Chromium is the open source base off of which Google Chrome is built. The open nature of Chromium forbids it to include support for proprietary codecs and demuxers. This means that the support for codecs in Chromium (and NW.js) is more limited than it is in Google Chrome.
Out of the box, Chromium/NW.js provides supports for the following audio and video codecs:
- VP8 & VP9
- PCM Audio (8bit, 16bit, 24bit & 32bit)
- Free Lossless Audio Codec (.flac)
- MP3 (since version 0.22.1)
These codecs can be used within the following multimedia containers:
Support for the above codecs and demuxers is provided by FFmpeg. The FFmpeg library can be compiled in various ways. One such way excludes all proprietary codecs. This is how it’s built by default for NW.js.
So, to include additional codecs, you would have to recompile FFmpeg with the appropriate options. However, recompiling software can be a complex proposition. Fortunately for us, a good soul has been doing that for each new version of NW.js that comes out.
So, here are the steps to go through to expand codec/container support in NW.js:
- Go to: https://github.com/iteufel/nwjs-ffmpeg-prebuilt/releases
- Download the zip that matches your version of NW.js and target architecture (32bit or 64bit)
- Unzip the package.
- Grab the ffmpeg.dll (Windows), libffmpeg.dylib (Mac) or libffmpeg.so (Linux) and overwrite the one found in the NW.js application folder:
- On Windows, the ffmpeg.dll file to overwrite is right at the root of the folder.
- On Mac, you have to right-click on nwjs.app and select Show Package Contents. This will allow you to navigate to and overwrite Contents/Versions/xx.xx.xx/libffmpeg.dylib.
- On Linux, the file to overwrite is lib/libffmpeg.so.
That’s it! Once you have done that, you should be able to play these additional types of content without issues:
- MP3 (if you had a version older than 0.22.1)
You will also be able to use additional multimedia containers:
- MP4 (.mp4 & .m4a)
- QuickTime (.mov)
If you so wish, you can also recompile FFmpeg yourself. You will find a simple script (and platform-specific instructions) on GitHub. You can also find more information on this topic in the NW.js documentation.
You should note that the ability to play back a proprietary codec does not dispense you to comply with its licensing terms. Here are some pointers for the most popular codecs:
- H.264: before using it, you should check out the H.264 licensing terms on the MPEG-LA website;
- AAC: per the license, unless you are creating an AAC encoder or decoder, you should be able to distribute AAC-encoded content without having to pay any fees;
- MP3: on April 27, 2017, the need to get a license from Technicolor and/or The Fraunhoffer IIS has been lifted. So, NW.js now includes support for MP3 decoding in its binary. The method described in this article still applies for older versions of NW.js that didn’t have built-in support (prior to version 0.22.1).
As of this writing, Chromium still does not include native support for MP3. NW.js has to use its own build of FFmpeg to support it.
You can get more information about the FFmpeg licensing terms on their website. Beware that this article does not contain legal advice. For the final word on licensing, you should consult a specialist.
Good morning Mr. Jean-Philippe I read on your page about Enabling audio / video codecs owners in NW.js, I would like to ask you a simple question that can help me a lot, I developed a game with nw.js, and to play the audio I’ll use WAV (windows PCM) format, I don´t now if I have to use ffmpeg, because wav is a uncompressed format, then maybe is not necessary use ffmpeg, I need to be sure because the application will work in Windows 7,8 and 10 with 32 and 64, the application will be sell and it’s not free, I can´ t make a mistake with this. If you help me with this doubt I’ll be very grateful, thank you…
Usually (but not necessarily), WAV files use PCM audio. This means that they should work without issues in NW.js on the listed platform. However, to be sure, you will have to do your own testing. Cheers!
is it possible to include the mpeg2video codec to the ffmpeg.dll ? Mayby someone could link to a precompiled version.
Thanks a lot
I do not know. I never had any use for MPEG2. Sorry.
Many thanks for quick reply.
i found the patch on https://github.com/iteufel/nwjs-ffmpeg-prebuilt/blob/master/patch/win/build_ffmpeg_proprietary_codecs.patch
where the mpeg2video would be needed to be added.
But sadly, i’m to capable to compile.
This is working perfectly for development environment(npm run devmac). If we are building for production using nwbuild(npm run prodwin), it is not working. Can you help me on this.
These are the starting scripts we are using
“devmac”: “nw release/”,
“prodmac”: “nwbuild –platforms osx64 –buildDir dist/ release/”,
“prodwin”: “nwbuild –platforms win64 –buildDir dist/ release/”,
“dev”: “webpack-dev-server –inline –progress –config build/webpack.dev.conf.js”,
“start”: “npm run dev”,
“build”: “node build/build.js”
Have you checked the information on this page?
It might help you solve the issue.
Thanks – Saved our day – Still helps in 2021