FFMPEG: extract audio with exact frame length of video
I've found the solution to my problem. The "apad" filter together with the "-shortest" tag will extend a sound track to the same length of a video track in ffmpeg.
This is how to use the apad filter in the windows command line (video and audio codec parameters are just examples):
"C:\Program Files (x86)\FFMPEG\bin\ffmpeg.exe" -i PATH/TO/INPUTVIDEO -filter_complex "[0:1]apad" -vcodec prores -profile:v 3 -acodec pcm_s24le -ar 48000 -shortest PATH/TO/OUTPUTVIDEO
More about the apad filter in the ffmpeg documentation: https://www.ffmpeg.org/ffmpeg-filters.html#apad
Related videos on Youtube
schmiddl
Updated on September 18, 2022Comments
-
schmiddl over 1 year
For a digital cinema package I need to extract the audio track (.wav, 48000 kHz, 16-bit) from QuickTime movies (in this case with Prores or Photo-JPEG codec). The catch is that the duration of the audio file has to have the exact same length in frames as the video track. The output wave file has to have 48000 kHz and 24-bit.
This is the command I tried:
"C:\Program Files (x86)\FFMPEG\bin\ffmpeg.exe" -i "input\path\file.mov" -vn -acodec pcm_s24le -ar 48000 "output\path\audio.wav"
I noticed that a lot of times the audio track is a couple of frames longer than the input QuickTime movie. But when I extract the audio with Adobe Media Encoder, it always has the exact same frame length. This can be verified by importing audio and video into After Effects, for example.
Example:
- input.mov consists of 1000 frames,
- ffmpeg output.wav has something like 1003 frames,
- AME output.wav has 1000 frames
Is there a way to make sure that ffmpeg does not produce audio tracks with a frame length shorter or longer than the original video?
Console Output:
ffmpeg started on 2015-01-29 at 20:32:44 Report written to "ffmpeg-20150129-203244.log" Command line: "C:\Program Files (x86)\FFMPEG\bin\ffmpeg.exe" -report -i "G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS.mov" -vn -acodec pcm_s24le -ar 48000 "G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS2.wav" ffmpeg version N-69375-g4155f2d Copyright (c) 2000-2015 the FFmpeg developers built on Jan 28 2015 22:14:02 with gcc 4.9.2 (GCC) configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-lzma --enable-decklink --enable-zlib libavutil 54. 18.100 / 54. 18.100 libavcodec 56. 21.101 / 56. 21.101 libavformat56. 19.100 / 56. 19.100 libavdevice56. 4.100 / 56. 4.100 libavfilter 5. 9.101 / 5. 9.101 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 1.100 / 1. 1.100 libpostproc53. 3.100 / 53. 3.100 Splitting the commandline. Reading option '-report' ... matched as option 'report' (generate a report) with argument '1'. Reading option '-i' ... matched as input file with argument 'G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS.mov'. Reading option '-vn' ... matched as option 'vn' (disable video) with argument '1'. Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'pcm_s24le'. Reading option '-ar' ... matched as option 'ar' (set audio sampling rate (in Hz)) with argument '48000'. Reading option 'G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS2.wav' ... matched as output file. Finished splitting the commandline. Parsing a group of options: global . Applying option report (generate a report) with argument 1. Successfully parsed a group of options. Parsing a group of options: input file G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS.mov. Successfully parsed a group of options. Opening an input file: G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS.mov. [mov,mp4,m4a,3gp,3g2,mj2 @ 000000000442d7c0] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100 [mov,mp4,m4a,3gp,3g2,mj2 @ 000000000442d7c0] ISO: File Type Major Brand: qt [mov,mp4,m4a,3gp,3g2,mj2 @ 000000000442d7c0] Before avformat_find_stream_info() pos: 890445130 bytes read:186331 seeks:4 [mov,mp4,m4a,3gp,3g2,mj2 @ 000000000442d7c0] All info found [mov,mp4,m4a,3gp,3g2,mj2 @ 000000000442d7c0] After avformat_find_stream_info() pos: 6279910 bytes read:1510667 seeks:6 frames:26 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS.mov': Metadata: major_brand : qt minor_version : 537199360 compatible_brands: qt creation_time : 2014-07-02 14:05:41 Duration: 00:02:23.00, start: 0.000000, bitrate: 49815 kb/s Stream #0:0(eng), 25, 1/24: Video: prores (apch / 0x68637061), yuv422p10le(bt709), 1920x1080, 49684 kb/s, SAR 1920:1920 DAR 16:9, 24 fps, 24 tbr, 24 tbn, 24 tbc (default) Metadata: creation_time : 2014-07-02 14:05:41 handler_name: Apple Alias Data Handler encoder : Apple ProRes 422 (HQ) timecode: 00:00:00:00 Stream #0:1(eng), 1, 1/48000: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 120 kb/s (default) Metadata: creation_time : 2014-07-02 14:05:41 handler_name: Apple Alias Data Handler timecode: 00:00:00:00 Stream #0:2(eng), 0, 1/24: Data: none (tmcd / 0x64636D74) (default) Metadata: creation_time : 2014-07-02 14:08:05 handler_name: Apple Alias Data Handler timecode: 00:00:00:00 Successfully opened the file. Parsing a group of options: output file G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS2.wav. Applying option vn (disable video) with argument 1. Applying option acodec (force audio codec ('copy' to copy stream)) with argument pcm_s24le. Applying option ar (set audio sampling rate (in Hz)) with argument 48000. Successfully parsed a group of options. Opening an output file: G:\01_dcp_pipeline\02_a_sound_ready\01_WOE_IS_ME_1080P_24FPS2.wav.
-
schmiddl about 9 yearsMy problem is not being caused by a change of the frame rate! It also happens with films where I don't touch the frame rate at all.
-
schmiddl about 9 yearsThe DCP workflow you propose on your website is not viable for me, because I need to automate all the necessary steps. Right now I have automated the scaling, frame rate conversion and format conversion with "filter_complex" in ffmpeg. The only thing that I can not get to work correctly is the audio extraction. Often I have to use Adobe Media Encoder to extract the audio, because it will always give me an audio track with the exact same frame length as the source film. ffmpeg will often produce an audio track that is between half a frame and 6 frames shorter than the source film.
-
FreezingFire almost 4 yearsThis solves the issue for short audio, but what about if the audio is longer? I just want to trim out the excess audio. How could I go about doing that?
-
schmiddl almost 4 yearsGet the duration of the video track. If the audio is longer than that, reencode it with the -ss 0.00 (start time zero) and -t (enter duration of video track in sec) flags. This will cut off the excess audio. The following code will extract the audio track from a video source with the exact same duration of the video track. You have to replace the variables with your own: "!FFMPEG!" -y -r !frame_rate! -i "!INFILE!" -ss 0.00 -t !video_duration! -acodec pcm_s24le -ar 48000 -r !frame_rate! "!OUTFILE!.wav"
-
Scott - Слава Україні over 3 yearsThis would be a better answer if you showed how to do it and explained why it works.