how to use ffmpeg to merge WebM audio (Opus) and MP4 video (H.264) into one MP4 file

Viewed 10825

I'm trying to merge WebM file with Opus audio + MP4 file with H.264 video into a MP4 file:

ffmpeg -i audio.webm -i video.mp4 -c copy output.mp4

However, I get an error:

[mp4 @ 0x56105a6d0a40] opus in MP4 support is experimental, add '-strict -2' if you want to use it.
Could not write header for output file #0 (incorrect codec parameters ?): Experimental feature

Complete log:

ffmpeg version n4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9.3.0 (Arch Linux 9.3.0-1)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, matroska,webm, from 'audio.webm':
  Metadata:
    ENCODER         : Lavf58.29.100
  Duration: 00:00:10.01, start: -0.007000, bitrate: 106 kb/s
    Stream #0:0: Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      ENCODER         : Lavc58.54.100 libopus
      DURATION        : 00:00:10.008000000
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:10.00, start: 0.000000, bitrate: 2796 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 2793 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
[mp4 @ 0x56105a6d0a40] track 1: codec frame size is not set
[mp4 @ 0x56105a6d0a40] opus in MP4 support is experimental, add '-strict -2' if you want to use it.
Could not write header for output file #0 (incorrect codec parameters ?): Experimental feature
Stream mapping:
  Stream #1:0 -> #0:0 (copy)
  Stream #0:0 -> #0:1 (copy)
    Last message repeated 1 times

Update 1

I tried this command:

ffmpeg -i audio.webm -i video.mp4 -c copy -strict experimental output.mp4

but got a new error:

[mov,mp4,m4a,3gp,3g2,mj2 @ 00000225b1eb07c0] Format mov,mp4,m4a,3gp,3g2,mj2 detected only with low score of 1, misdetection possible! [mov,mp4,m4a,3gp,3g2,mj2 @ 00000225b1eb07c0] moov atom not found inputVideo.mp4: Invalid data found when processing input


Update 2

I found that my input video is corrupted which caused the error in Update 1.

So once I ran the command in Update 1, it did give me a bigger mp4 file, but the file is still no audio.

2 Answers

Problem #1: Opus in MP4

Add -strict -2 (or the alias -strict experimental) as mentioned in your log:

[mp4 @ 0x5630ee4eb000] opus in MP4 support is experimental, add '-strict -2' if you want to use it.

Example command:

ffmpeg -i audio.webm -i video.mp4 -c copy -strict experimental output.mp4

However, newer versions of ffmpeg no longer consider Opus in MP4 to be experimental, so if you upgrade you won't need to add -strict.

Latest release version as of this answer is 4.2.2. Use a version from the git master branch or wait for 4.3 to skip using -strict.


Problem #2: Invalid data found when processing input

The input file is corrupt. Unfortunately there is nothing ffmpeg can do about this.


Problem #3: No audio

This means that your player, browser, or device does not support Opus audio in MP4 container. You'll need to re-encode the audio to AAC:

ffmpeg -i audio.webm -i video.mp4 -c copy -c:a aac output.mp4

I found the answer:

ffmpeg -i video.mp4 -i audio.webm -c:v copy video480p.mp4

also, if you would like to re-encode the audio

ffmpeg -i video.mp4 -i audio.webm -c:v copy -c:a aac video480p.mp4

As a beginner, I have no idea about what does -c:v mean, so I found this document which tells all about it. http://ffmpeg.org/ffmpeg-all.html#Stream-specifiers

Related