如题,我现在有一个视频文件(既有视频又有音频,放在D盘的vedio目录下的VID_20200223_153448.mp4),但我只想要拿出音频,那就需要抽取出来放到另一个文件中(D盘的pcm目录下的test.pcm),现在我们用ffmpeg命令操作一下:
D:\>ffmpeg -y -i d:\vedio\VID_20200223_153448.mp4 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 d:\pcm\test.pcm ffmpeg version git-2020-03-06-cfd9a65 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.2.1 (GCC) 20200122 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-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf libavutil 56. 42.100 / 56. 42.100 libavcodec 58. 73.102 / 58. 73.102 libavformat 58. 39.101 / 58. 39.101 libavdevice 58. 9.103 / 58. 9.103 libavfilter 7. 77.100 / 7. 77.100 libswscale 5. 6.100 / 5. 6.100 libswresample 3. 6.100 / 3. 6.100 libpostproc 55. 6.100 / 55. 6.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'd:\vedio\VID_20200223_153448.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2020-02-23T07:34:48.000000Z com.android.version: 9 Duration: 00:08:30.66, start: 0.000000, bitrate: 9987 kb/s Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 9783 kb/s, SAR 1:1 DAR 16:9, 30.18 fps, 30 tbr, 90k tbn, 60 tbc (default) Metadata: rotate : 90 creation_time : 2020-02-23T07:34:48.000000Z handler_name : VideoHandle Side data: displaymatrix: rotation of -90.00 degrees Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 192 kb/s (default) Metadata: creation_time : 2020-02-23T07:34:48.000000Z handler_name : SoundHandle Stream mapping: Stream #0:1 -> #0:0 (aac (native) -> pcm_s16le (native)) Press [q] to stop, [?] for help Output #0, s16le, to 'd:\pcm\test.pcm': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 com.android.version: 9 encoder : Lavf58.39.101 Stream #0:0(eng): Audio: pcm_s16le, 16000 Hz, mono, s16, 256 kb/s (default) Metadata: creation_time : 2020-02-23T07:34:48.000000Z handler_name : SoundHandle encoder : Lavc58.73.102 pcm_s16le size= 15955kB time=00:08:30.56 bitrate= 256.0kbits/s speed= 421x video:0kB audio:15955kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
可以看到生成了音频文件——D:\pcm\test.pcm。我们看到这个命令干了抽离音频的活:
ffmpeg -y -i d:\vedio\VID_20200223_153448.mp4 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 d:\pcm\test.pcm
它主要参数说明如下:
-y 覆盖输出文件 -i 输入要处理的视频文件路径 -f 强迫采用格式fmt -ac 设置通道(缺省为1) -ar 设置音频采样率
最后看下用java代码怎么执行上面的命令:
package com.wlf.ffmpeg.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Ffmpeg处理类 * * @author wlf * @since 2020/3/9 */ public class FfmpegUtils { private static final String ffmpegPath = "D:\\ffmpeg\\bin\\ffmpeg.exe"; public static void vedio2pcm(String vedioPath, String pcmPath) throws IOException, InterruptedException { List<String> commends = new ArrayList<String>(); commends.add(ffmpegPath); commends.add("-y"); commends.add("-i"); commends.add(vedioPath); commends.add("-acodec"); commends.add("pcm_s16le"); commends.add("-f"); commends.add("s16le"); commends.add("-ac"); commends.add("1"); commends.add("-ar"); commends.add("16000"); commends.add(pcmPath); ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command(commends); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); process.waitFor(); } public static void main(String[] args) throws IOException, InterruptedException { vedio2pcm("D:\\vedio\\VID_20200223_153448.mp4", "D:\\pcm\\PCM_20200223_153448.pcm"); } }
运行下上面的代码,我们可以得到一个跟test.pcm一样的音频文件: