命令行操控音频

我记得之前想裁剪点音视频的时候,都是通过第三方桌面剪辑软件操控的,现在我们可以通过命令行来玩一下

1命令简介

Snip20210425_5.png

可以看到在我的电脑上面关于FFmpeg的命令有3个比较重要的,分别是ffmpeg,ffplay,ffprobe

1.1 ffmpeg 的使用
ffmpeg 的主要作用:对音视频解析编解码
举个例子,通过ffmpeg 可以将mp3 格式的转成wav文件格式
ffmpeg -i xx.mp3 yy.wav

可以通过ffmpeg -h 、ffmpeg -h long、ffmpeg -h full 来查看

yper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Getting help:
    -h      -- print basic options
    -h long -- print more options
    -h full -- print all options (including all format and codec specific options, very long)
    -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol
    See man ffmpeg for detailed description of the options.

稍微解释一下

  • options: 全局参数
  • infile options: 输入文件参数
  • infile: 输入文件
  • outfile options: 输出文件参数
  • outfile: 输出文件
    更加详细的操作指令,可以参考官方链接ffmpeg指令
1.2 ffprobe
ffprobe 的主要作用:查看音视频的参数信息
 songlin@feng-sl  /usr/local/bin   master ±  ffprobe
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Simple multimedia streams analyzer
usage: ffprobe [OPTIONS] [INPUT_FILE]

You have to specify one input file.
Use -h to get full help or, even better, run 'man ffprobe'.

更多详细用法也可以参考ffprobe官方链接

1.3ffplay
ffplay的主要作用:播放音视频
ffplay xxx.mp3
 ✘ songlin@feng-sl  /usr/local/bin   master ±  ffplay
ffplay version 4.3.2 Copyright (c) 2003-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Simple media player
usage: ffplay [options] input_file

An input file must be specified
Use -h to get full help or, even better, run 'man ffplay'

更详细的使用信息可以参考ffplay官方链接

另外有一个叫做hide_banner 的指令可以避免输出一些冗余信息,如下面所示

 ✘ songlin@feng-sl  /usr/local/bin   master ±  ffprobe /Users/songlin/Music/网易云音乐/Various\ Artists\ -\ The\ Green\ Grass\ Grew\ All\ Around.mp3
ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Input #0, mp3, from '/Users/songlin/Music/网易云音乐/Various Artists - The Green Grass Grew All Around.mp3':
  Metadata:
    encoder         : Lavf56.4.101
    disc            : 1
    track           : 10
    artist          : Various Artists
    comment         : 163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/ZcfTzfN1xHT6iwZxsZZXgemKny6LV6u/V8mlF23vdviS3CcD/EnUZGP30CsQAX/wNC/tMz+O+OHyvBRMGis70PeMNdV7GK3IA4bOKUvLKUYgTTAZ+ZK1ItDyq0PeLRSrkK2aLoiZydpNDBG1Wvq3xz50f3qIUs2Ww+7sM4thBwbZMHLLLuN3UNzTzgfxS3nlcCnLq73GuUUzO3GIOmIU
    title           : The Green Grass Grew All Around
    album           : 迪士尼最爱儿歌系列1
  Duration: 00:03:44.99, start: 0.025056, bitrate: 327 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s
    Stream #0:1: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 500x500 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc (attached pic)
    Metadata:
      comment         : Other
 songlin@feng-sl  /usr/local/bin   master ±  ffprobe /Users/songlin/Music/网易云音乐/Various\ Artists\ -\ The\ Green\ Grass\ Grew\ All\ Around.mp3
 ✘ songlin@feng-sl  /usr/local/bin   master ±  ffprobe -hide_banner /Users/songlin/Music/网易云音乐/Various\ Artists\ -\ The\ Green\ Grass\ Grew\ All\ Around.mp3
Input #0, mp3, from '/Users/songlin/Music/网易云音乐/Various Artists - The Green Grass Grew All Around.mp3':
  Metadata:
    encoder         : Lavf56.4.101
    disc            : 1
    track           : 10
    artist          : Various Artists
    comment         : 163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/ZcfTzfN1xHT6iwZxsZZXgemKny6LV6u/V8mlF23vdviS3CcD/EnUZGP30CsQAX/wNC/tMz+O+OHyvBRMGis70PeMNdV7GK3IA4bOKUvLKUYgTTAZ+ZK1ItDyq0PeLRSrkK2aLoiZydpNDBG1Wvq3xz50f3qIUs2Ww+7sM4thBwbZMHLLLuN3UNzTzgfxS3nlcCnLq73GuUUzO3GIOmIU
    title           : The Green Grass Grew All Around
    album           : 迪士尼最爱儿歌系列1
  Duration: 00:03:44.99, start: 0.025056, bitrate: 327 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s
    Stream #0:1: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 500x500 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc (attached pic)
    Metadata:
      comment         : Other

2.命令行录音

2.1 查看可用列表设备
songlin@feng-sl  /usr/local/bin   master ±  ffmpeg -devices
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  avfoundation    AVFoundation input device
 D  lavfi           Libavfilter virtual input device
  E sdl,sdl2        SDL2 output device
 D  x11grab         X11 screen capture, using XCB

从上面的输出结果中,可以看到列表中有一个avfoundation的选项,这个就是Mac平台的多媒体系统库,我们可以借助这个avfoundation去操作多媒体输入设备

2.2 查看avfoundation 支持的设备列表
 songlin@feng-sl  /usr/local/bin   master ±  ffmpeg -f avfoundation -list_devices true -i ''
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
[AVFoundation indev @ 0x7fcdc3704580] AVFoundation video devices:
[AVFoundation indev @ 0x7fcdc3704580] [0] FaceTime HD Camera
[AVFoundation indev @ 0x7fcdc3704580] [1] Capture screen 0
[AVFoundation indev @ 0x7fcdc3704580] [2] Capture screen 1
[AVFoundation indev @ 0x7fcdc3704580] AVFoundation audio devices:
[AVFoundation indev @ 0x7fcdc3704580] [0] Built-in Microphone
: Input/output error
  • f avfoundation: avfoundation 支持的
  • list_devices true 打印所有的设备
  • -i ' ' 或者 -i "" : 立即退出
2.3 指定设备进行录音
 ✘ songlin@feng-sl  ~   master ±✚  ffmpeg -f avfoundation -i :0 out.wav
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Input #0, avfoundation, from ':0':
  Duration: N/A, start: 52087.499524, bitrate: 2822 kb/s
    Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_f32le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'out.wav':
  Metadata:
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=    1986kB time=00:00:11.84 bitrate=1373.9kbits/s speed=   1x
video:0kB audio:1986kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.003835%
Exiting normally, received signal 2.

使用Ctrl+C 终止录音,可以看到在我的电脑上面是采用, 44100 Hz 采样率,16位深度( pcm_s16le),比特率 2822 kb/s,stereo双声道

2.4 测试录音效果
 songlin@feng-sl  ~   master ±✚  ffplay out.wav
ffplay version 4.3.2 Copyright (c) 2003-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Input #0, wav, from 'out.wav':
  Metadata:
    encoder         : Lavf58.45.100
  Duration: 00:00:11.53, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
   6.37 M-A:  0.000 fd=   0 aq=  180KB vq=    0KB sq=    0B f=0/0

可以发现刚刚我们录制的东西已经成功了

2.5 avfoundation 参数使用
✘ songlin@feng-sl  ~   master ±✚  ffmpeg -h demuxer=avfoundation
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2_4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  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
Demuxer avfoundation [AVFoundation input device]:
AVFoundation indev AVOptions:
  -list_devices          .D........ list available devices (default false)
  -video_device_index         .D........ select video device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
  -audio_device_index         .D........ select audio device by index for devices with same name (starts at 0) (from -1 to INT_MAX) (default -1)
  -pixel_format          .D........ set pixel format (default yuv420p)
  -framerate          .D........ set frame rate (default "ntsc")
  -video_size         .D........ set video size
  -capture_cursor        .D........ capture the screen cursor (default false)
  -capture_mouse_clicks     .D........ capture the screen mouse clicks (default false)
  -capture_raw_data      .D........ capture the raw data from device connection (default false)
  -drop_late_frames      .D........ drop frames that are available later than expected (default true)

你可能感兴趣的:(命令行操控音频)