FFMPEG从音视频流中抽取PCM格式音频

  如题,我现在有一个视频文件(既有视频又有音频,放在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 输出文件格式
-ac 设置通道(缺省为1)
-ar 设置音频采样率

  最后看下用java代码怎么执行上面的命令:

package com.wlf.ffmpeg.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
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 commends = new ArrayList();
        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);
        processCmd(commends);
    }


    private static void processCmd(List commends) throws InterruptedException, IOException {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(commends);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();

        System.out.println("--- 开始执行FFmpeg指令:--- 执行线程名:" + processBuilder.toString());

        BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream())));
        BufferedReader err = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getErrorStream())));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }

        while ((line = err.readLine()) != null) {
            System.err.println(line);
        }

        if (process.waitFor() != 0 && process.exitValue() == 1) {
            System.err.println("Failed!");
        }

        in.close();

        // 输出执行的命令信息
        String cmdStr = Arrays.toString(commends.toArray()).replace(",", "");
        System.out.println("--- 已执行的FFmepg命令: ---" + cmdStr);
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        vedio2pcm("D:\\vedio\\VID_20200223_153448.mp4", "D:\\pcm\\test3171033.pcm");
    }
}

  运行下上面的代码,我们可以得到一个跟test.pcm一样的音频文件,控制台输出:

D:\Dev\Java\jdk1.8.0_102\bin\java.exe "-javaagent:D:\Dev\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=59026:D:\Dev\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath D:\Dev\Java\jdk1.8.0_102\jre\lib\charsets.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\deploy.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\javaws.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\jce.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\jfr.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\jsse.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\management-agent.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\plugin.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\resources.jar;D:\Dev\Java\jdk1.8.0_102\jre\lib\rt.jar;E:\workspace\greendill\target\classes;E:\Users\wulf\.m2\repository\org\projectlombok\lombok\1.18.10\lombok-1.18.10.jar;E:\Users\wulf\.m2\repository\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar com.wlf.ffmpeg.util.FfmpegUtils
--- 开始执行FFmpeg指令:--- 执行线程名:java.lang.ProcessBuilder@14ae5a5
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\test3171033.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=    5376kB time=00:02:55.16 bitrate= 251.4kbits/s speed= 350x    
size=   10496kB time=00:05:41.86 bitrate= 251.5kbits/s speed= 341x    
size=   15616kB time=00:08:22.34 bitrate= 254.7kbits/s speed= 334x    
size=   15955kB time=00:08:30.56 bitrate= 256.0kbits/s speed= 332x    
video:0kB audio:15955kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
--- 已执行的FFmepg命令: ---[D:\ffmpeg\bin\ffmpeg.exe -y -i D:\vedio\VID_20200223_153448.mp4 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 D:\pcm\test3171033.pcm]

Process finished with exit code 0

你可能感兴趣的:(jdk,ffmpeg,jre,java,javac)