简体   繁体   中英

How to use ffmpeg to change the h264 properties, yuv, fps, tbr, tbn, and tbc of an mp4 file?

Using ffmpeg, I would like to convert a video file such that its video stream changes like so:

  • Current video stream: Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 2560x1080 [SAR 4:3 DAR 256:81], 60 kb/s, 15 fps, 15 tbr, 15360 tbn, 30 tbc (default)
  • Target video stream: Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 2560x1080, 272 kb/s, 20 fps, 20 tbr, 10240 tbn, 40 tbc (default)

(values that differ: h264 properties, yuv, fps, tbr, tbn, and tbc)

... and its audio stream changes like so:

  • Current audio stream: Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 191 kb/s (default)
  • Target audio stream: Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 69 kb/s (default)

(values that differ: aac properties)

How can I do this?


Detailed version

I would like to be able to concatenate, without re-encoding, main.mp4 , followed by outro.mp4 , using the following commands:

echo "file 'main.mp4'" > concat.txt
echo "file 'outro.mp4'" >> concat.txt
ffmpeg \
  -f concat \
  -safe 0 \
  -i concat.txt \
  -c copy \
  concat.mp4

What results is a file which plays till the end of main.mp4 , and then the video freezes, and I hear the audio of outro.mp4 . The same video frame then stays on, with no sound, for an extra ~30 minutes (end time shown in VLC).

My assumption is that these files are incompatible with each other in some way that prevents them from being concatenated using -codec copy (without re-encoding).

Here is the ffprobe output for main.mp4 :

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'main.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.45.100
  Duration: 01:13:00.65, start: 0.000000, bitrate: 348 kb/s
    Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 2560x1080, 272 kb/s, 20 fps, 20 tbr, 10240 tbn, 40 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 69 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

Here is the ffprobe output for outro.mp4 :

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'outro.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.45.100
  Duration: 00:00:04.12, start: 0.000000, bitrate: 254 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 2560x1080 [SAR 4:3 DAR 256:81], 60 kb/s, 15 fps, 15 tbr, 15360 tbn, 30 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 191 kb/s (default)
    Metadata:
      handler_name    : Stereo

How can I convert outro.mp4 , such that I may concatenate the files without re-encoding?

Note that I am OK with re-encoding the outro.mp4 on its own, I simply want to avoid re-encoding during the concatenation step, and avoid re-encoding main.mp4 .


ffmpeg build

$ ffmpeg -version
ffmpeg version 4.3-2~18.04.york0 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --prefix=/usr --extra-version='2~18.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
libavutil      56. 51.100 / 56. 51.100
libavcodec     58. 91.100 / 58. 91.100
libavformat    58. 45.100 / 58. 45.100
libavdevice    58. 10.100 / 58. 10.100
libavfilter     7. 85.100 /  7. 85.100
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  7.100 /  5.  7.100
libswresample   3.  7.100 /  3.  7.100
libpostproc    55.  7.100 / 55.  7.100

The conversion command for outro.mp4 is

ffmpeg -i outro.mp4 -pix_fmt yuv444p -profile high444 -bf 0 -video_track_timescale 10240 -ac 1 out.mp4

Add -vf "setdar=dar=0,setsar=sar=0" , -r 20 , and -qscale:v 2 to the above command to get the exact intended output.

ffmpeg \
  -i outro.mp4 \
  -vf "setdar=dar=0,setsar=sar=0" \
  -pix_fmt yuv444p \
  -profile:v high444 \
  -bf 0 \
  -video_track_timescale 10240 \
  -r 20 \
  -qscale:v 2 \
  -ac 1 \
  out.mp4

Side note: If the video contains audio as well, and using the copy codec, be sure to ensure that those are matching too. For example use -ar to set the audio rate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM