13

I am desperately trying to cut an MP4 video with ffmpeg, following this [example] https://github.com/facebookresearch/VideoPose3D/blob/master/INFERENCE.md

ffmpeg -i P1060513copy.mp4 -ss 3:12 -to 3:30 -c copy output.mp4

I am getting this error

ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
  configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'P1060513.MP4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: mp42avc1
    creation_time   : 2017-11-15 10:10:14
  Duration: 00:03:52.00, start: 0.000000, bitrate: 51908 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 50365 kb/s, 24 fps, 24 tbr, 360k tbn, 48 tbc (default)
    Metadata:
      creation_time   : 2017-11-15 10:10:14
      timecode        : 01:39:08:00
    Stream #0:1(und): Audio: pcm_s16be (twos / 0x736F7774), 48000 Hz, 2 channels, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2017-11-15 10:10:14
      timecode        : 01:39:08:00
    Stream #0:2(und): Data: none (tmcd / 0x64636D74), 0 kb/s (default)
    Metadata:
      creation_time   : 2017-11-15 10:10:14
      timecode        : 01:39:08:00
[mp4 @ 0x18bc2c0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0x18bc2c0] Codec for stream 1 does not use global headers but container format requires global headers
[mp4 @ 0x18bc2c0] Could not find tag for codec pcm_s16be in stream #1, codec not currently supported in container
Output #0, mp4, to 'output.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: mp42avc1
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 50365 kb/s, 24 fps, 24 tbr, 360k tbn, 360k tbc (default)
    Metadata:
      creation_time   : 2017-11-15 10:10:14
      timecode        : 01:39:08:00
    Stream #0:1(und): Audio: pcm_s16be (twos / 0x736F7774), 48000 Hz, stereo, 1536 kb/s (default)
    Metadata:
      creation_time   : 2017-11-15 10:10:14
      timecode        : 01:39:08:00
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

To avoid this error I converted the file in mkv and cut it (works perfectly) and then I wanted to convert it back to MP4 but ended with the same error.

Can anyone help me?

Thank you

1
  • 8
    Save to mov or change -c copy to -c:v copy
    – Gyan
    Oct 8, 2019 at 14:30

3 Answers 3

8

It seems like this is known behavior and intended by the ffmpeg developers. https://trac.ffmpeg.org/ticket/3818

Apparently the MP4 norm does not include the pcm_s16be codec and hence ffmpeg refuses to create such a file.

It's very inconvenient unfortunately. When I cut while only copying the video code, e.g.

ffmpeg -i uncut.MP4 -ss 00:00:10.440 -to 00:00:28.440 -vcodec copy cut.MP4

I get a noticeable offset in the audio stream.

3
  • 5
    Copying just the video stream works for me without any noticeable offset in the video, e.g., using ffmpeg -i uncut.mp4 -ss 00:02:27 -to 00:02:36 c:v copy cut.mp4 seems to work fine, re-encoding the audio to AAC without any offset. This is in the current ffmpeg 4.3.1. Thanks for your posted answer, it helped me get past the issue quickly.
    – Basil
    Aug 17, 2020 at 14:34
  • 1
    @Basil's answer worked, altho on his comment it seems like he missed a hyphen, should've been -c:v copy, so: ffmpeg -i input.mp4 -ss 00:01:23 -to 00:03:21 -c:v copy output.mp4
    – Seraf
    Dec 26, 2022 at 16:33
  • @Seraf you're right, thanks for catching that!
    – Basil
    Dec 26, 2022 at 19:16
2

Save to mov or change -c copy to -c:v copy

# Changing to -c:v copy
$ ffmpeg -i P1060513copy.mp4 -ss 3:12 -to 3:30 -c:v copy output.mp4

# Changing to .mov
$ ffmpeg -i P1060513copy.mp4 -ss 3:12 -to 3:30 -c copy output.mov
3
  • mov thing worked thanks.
    – Santhosh
    Nov 25, 2022 at 11:55
  • 1
    -c:v copy worked thanks.
    – shantanuo
    Apr 29 at 6:50
  • I was expecting this to lose audio because of the missing -c:a copy, but... it doesn't? Aug 16 at 20:34
0

Reason is that ffmpeg is following strict mp4 compliance and will not process (though it has the ability to process both the video and the audio codecs).

What I did in this scenario is to pre-process the files by filtering out all the mp4s that use pcm_s16be (standard in Sony alpha cameras) and encoding only the audio into aac and leaving video stream un-encoded (to preserve the quality)

TLDR: (Do both these steps)

ffmpeg -i P1060513copy.mp4 -acodec aac -vcodec copy temp_P1060513copy.mp4

ffmpeg -i temp_P1060513copy.mp4 -ss 3:12 -to 3:30 -c copy P1060513copy_cut.mp4

My requirement was slightly different because I needed to rotate my videos using ffmpeg, but ffmpeg would not allow me since the input has PCM (because some Sony cameras have that). So before the rotation, I first needed to convert PCM to AAC. (Reason I choose AAC instead of flac is because many softwares dont allow smooth edits with flac - eg. DaVinci)

Following is my code to achieve the above:

1 - To identify the audio codec (before using ffmpeg, am using ffprobe to gather all the metadata and help me filter)

ffprobe_command = "ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1  "
status, audio_codec = subprocess.getstatusoutput(ffprobe_command+ " " + "\"" + one_file + "\" ")

2 - To encode only those files identified above

if audio_codec == 'pcm_s16be':
    print("pcm_s16be found, hence processing further")
    status, converted_result = subprocess.getstatusoutput("ffmpeg -i " + "\"" + str(one_file) + "\"" +" -acodec aac -vcodec copy " + "\"" + "pcm_"  + one_file + "\"")

Note: Am running this as part of a larger python program, and this runs inside a loop, hence the references to 'one_file' etc.

If the above python snippet is confusing you, you can use this for individual files.

ffmpeg -i input_file.mp4 -acodec aac -vcodec copy output_file.mp4

After both these steps the mp4 will now have aac as audio codec and ffmpeg will allow this for any downstream encodes.

You can check the section under Stream Mapping to confirm that only the audio is re-encoded.

pcm_s16be found, hence processing further

ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3.1 (GCC) 20200523 configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt 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 [mov,mp4,m4a,3gp,3g2,mj2 @ 0000022621729280] st: 0 edit list: 1 Missing key frame while searching for timestamp: 1000 [mov,mp4,m4a,3gp,3g2,mj2 @ 0000022621729280] st: 0 edit list 1 Cannot find an index entry before timestamp: 1000. Guessed Channel Layout for Input Stream #0.1 : stereo Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4': Metadata: major_brand : XAVC minor_version : 16785407 compatible_brands: XAVCmp42iso2 creation_time : 2018-12-15T00:56:11.000000Z Duration: 00:00:14.40, start: 0.000000, bitrate: 95986 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/iec61966-2-4), 1920x1080 [SAR 1:1 DAR 16:9], 94241 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default) Metadata: creation_time : 2018-12-15T00:56:11.000000Z handler_name : Video Media Handler encoder : AVC Coding Stream #0:1(und): Audio: pcm_s16be (twos / 0x736F7774), 48000 Hz, stereo, s16, 1536 kb/s (default) Metadata: creation_time : 2018-12-15T00:56:11.000000Z handler_name : Sound Media Handler Stream #0:2(und): Data: none (rtmd / 0x646D7472), 204 kb/s (default) Metadata: creation_time : 2018-12-15T00:56:11.000000Z handler_name : Timed Metadata Media Handler timecode : 00:03:24:12

Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (pcm_s16be (native) -> aac (native)) Press [q] to stop, [?] for help Output #0, mp4, to 'output.mp4': Metadata: major_brand : XAVC minor_version : 16785407 compatible_brands: XAVCmp42iso2 encoder : Lavf58.29.100 Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/bt709/iec61966-2-4), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 94241 kb/s, 25 fps, 25 tbr, 25k tbn, 25k tbc (default) Metadata: creation_time : 2018-12-15T00:56:11.000000Z handler_name : Video Media Handler encoder : AVC Coding Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2018-12-15T00:56:11.000000Z handler_name : Sound Media Handler encoder : Lavc58.54.100 aac frame= 78 fps=0.0 q=-1.0 size= 32256kB time=00:00:03.04 bitrate=86920.4kbits/s speed=5.99x
frame= 140 fps=130 q=-1.0 size= 57856kB time=00:00:05.52 bitrate=85861.1kbits/s speed=5.14x
frame= 218 fps=139 q=-1.0 size= 94208kB time=00:00:08.64 bitrate=89322.8kbits/s speed=5.49x
frame= 288 fps=139 q=-1.0 size= 124160kB time=00:00:11.44 bitrate=88908.7kbits/s speed= 5.5x
frame= 357 fps=138 q=-1.0 size= 160256kB time=00:00:14.20 bitrate=92451.7kbits/s speed= 5.5x
frame= 360 fps=137 q=-1.0 Lsize= 165896kB time=00:00:14.40 bitrate=94376.2kbits/s speed=5.48x
video:165658kB audio:226kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.006983% [aac @ 000002262173a940] Qavg: 176.176

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.