音视频开发-ffmpeg指令(1)

ffmpeg基本信息查询命令

在这里插入图片描述

Last login: Mon Jun 24 11:35:04 on ttys002
gensees-iMac:~ gensee$ ffmpeg -version
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 8.1.0 (clang-802.0.38)
configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
libavutil      56. 29.100 / 56. 29.100
libavcodec     58. 53.100 / 58. 53.100
libavformat    58. 27.103 / 58. 27.103
libavdevice    58.  7.100 / 58.  7.100
libavfilter     7. 55.100 /  7. 55.100
libswscale      5.  4.101 /  5.  4.101
libswresample   3.  4.100 /  3.  4.100
libpostproc    55.  4.100 / 55.  4.100

gensees-iMac:~ gensee$ ffmpeg -devices
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.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

录屏命令

ffmpeg -f avfoundation -i 1 -r 30 out.yuv

-f 指定使用 avfoundation 采集数据。
-i 指定从哪儿采集数据,它是一个文件索引号。在我的MAC上,1代表桌面(可以通过上面的命令查询设备索引号)。
-r 指定帧率。按ffmpeg官方文档说-r与-framerate作用相同,但实际测试时发现不同。-framerate 用于限制输入,而-r用于限制输出。

//开始录屏命令
gensees-iMac:~ gensee$ ffmpeg -f avfoundation -i 1 -r 30 out.yuv
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
[avfoundation @ 0x7ffb51004000] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7ffb51004000] Supported pixel formats:
[avfoundation @ 0x7ffb51004000]   uyvy422
[avfoundation @ 0x7ffb51004000]   yuyv422
[avfoundation @ 0x7ffb51004000]   nv12
[avfoundation @ 0x7ffb51004000]   0rgb
[avfoundation @ 0x7ffb51004000]   bgr0
[avfoundation @ 0x7ffb51004000] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7ffb51004000] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '1':
  Duration: N/A, start: 319880.981333, bitrate: N/A
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, 1000k tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> rawvideo (native))
Press [q] to stop, [?] for help
Output #0, rawvideo, to 'out.yuv':
  Metadata:
    encoder         : Lavf58.27.103
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, q=2-31, 995328 kb/s, 30 fps, 30 tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.53.100 rawvideo
frame=   19 fps=0.0 q=-0.0 size=   76800kB time=00:00:00.63 bitrate=993388.3kbitframe=   34 fps= 33 q=-0.0 size=  137472kB time=00:00:01.13 bitrate=993680.3kbitframe=   49 fps= 32 q=-0.0 size=  198400kB time=00:00:01.63 bitrate=995077.4kbitframe=   64 fps= 31 q=-0.0 size=  259072kB time=00:00:02.13 bitrate=994836.6kbitframe=   80 fps= 31 q=-0.0 size=  323840kB time=00:00:02.66 bitrate=994836.4kbitframe=   96 fps= 31 q=-0.0 size=  388608kB time=00:00:03.20 bitrate=994836.5kbitframe=  112 fps= 31 q=-0.0 size=  453376kB time=00:00:03.73 bitrate=994836.6kbitframe=  126 fps= 30 q=-0.0 size=  510208kB time=00:00:04.20 bitrate=995148.6kbitframe=  127 fps= 30 q=-0.0 Lsize=  514350kB time=00:00:04.23 bitrate=995328.1kbits/s speed=1.01x    
video:514350kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
Exiting normally, received signal 2.
//这里control+c暂停
//然后播放yuv数据,需要指定分辨率,分辨率可以从上面录屏时的打印日志取到
gensees-iMac:~ gensee$ ffplay -s 1920x1080 /Users/gensee/out.yuv 
ffplay version N-94063-g86f04b918c Copyright (c) 2003-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Option -s is deprecated, use -video_size.
[rawvideo @ 0x7fd7ee83be00] Estimating duration from bitrate, this may be inaccurate
Input #0, rawvideo, from '/Users/gensee/out.yuv':
  Duration: 00:00:06.76, start: 0.000000, bitrate: 623306 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1920x1080, 622080 kb/s, 25 tbr, 25 tbn, 25 tbc
   2.63 M-V: -0.039 fd=   2 aq=    0KB vq=18225KB sq=    0B f=0/0   
//这里发现yuv数据播放是花屏乱码,发现录制时采用的是uyvy422的yuv数据类型,而我们默认以yuv420p播放,这里使用-pix_fmt xxxx 指定像素格式,因为pixel是像素,format格式
gensees-iMac:~ gensee$ ffplay -s 1920x1080 -pix_fmt uyvy422 /Users/gensee/out.yuv 
ffplay version N-94063-g86f04b918c Copyright (c) 2003-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Option -s is deprecated, use -video_size.
Option -pix_fmt is deprecated, use -pixel_format.
[rawvideo @ 0x7f95290afa00] Estimating duration from bitrate, this may be inaccurate
Input #0, rawvideo, from '/Users/gensee/out.yuv':
  Duration: 00:00:05.08, start: 0.000000, bitrate: 829440 kb/s
    Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, 829440 kb/s, 25 tbr, 25 tbn, 25 tbc
   3.37 M-V: -0.040 fd=   2 aq=    0KB vq=16200KB sq=    0B f=0/0   
gensees-iMac:~ gensee$ 
//到这里播放成功

如何查询设备索引号

ffmpeg -f avfoundation -list_devices true -i ""
gensees-iMac:~ gensee$ ffmpeg -f avfoundation -list_devices true -i ""
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
//视频设备
[AVFoundation input device @ 0x7fc917e03dc0] AVFoundation video devices:
//摄像头
[AVFoundation input device @ 0x7fc917e03dc0] [0] FaceTime HD Camera (Built-in)
//屏幕
[AVFoundation input device @ 0x7fc917e03dc0] [1] Capture screen 0
//音频设备
[AVFoundation input device @ 0x7fc917e03dc0] AVFoundation audio devices:
[AVFoundation input device @ 0x7fc917e03dc0] [0] Built-in Microphone
: Input/output error
gensees-iMac:~ gensee$ 

录制声音

ffmpeg -f avfoundation -i :0 out.wav

:0 表示音频设备

gensees-iMac:desktop gensee$ ffmpeg -f avfoundation -i :0 out.wav
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Input #0, avfoundation, from ':0':
  Duration: N/A, start: 320614.552608, 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.27.103
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc58.53.100 pcm_s16le
size=    1946kB time=00:00:11.30 bitrate=1409.8kbits/s speed=   1x    
video:0kB audio:1946kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.003914%
Exiting normally, received signal 2.

录制视频和声音

ffmpeg -f avfoundation -i 1:0 -r 30 -c:v libx264 -c:a libfdk_aac -profile:a aac_he_v2 -b:a 32k out.flv

-i 1:0 冒号前面的 “1” 代表的屏幕索引号。冒号后面的"0"代表的声音索相号。
-c:v 与参数 -vcodec 一样,表示视频编码器。c 是 codec 的缩写,v 是video的缩写。
-crf 是 x264 的参数。 0 表式无损压缩。
-c:a 与参数 -acodec 一样,表示音频编码器。
-profile 是 fdk_aac 的参数。 aac_he_v2 表式使用 AAC_HE v2 压缩数据。
-b:a 指定音频码率。 b 是 bitrate的缩写, a是 audio的缩写。

多媒体格式转换

ffmpeg -i out.flv -vcodec copy -acodec copy out.mp4

-i 输入文件
-vcodec 表示视频编码 copy表示复制,即参数不做调整
-acodec 表示音频频编码 copy表示复制,即参数不做调整

gensees-iMac:study gensee$ ffmpeg -i out.flv -vcodec copy -acodec copy out.mp4
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Input #0, flv, from 'out.flv':
  Metadata:
    major_brand     : qt  
    minor_version   : 512
    compatible_brands: qt  
    artist          : yinyuetai.com
    album           : Yinyuetai
    date            : 11/11/16 22:40:13
    comment         : Yinyuetai-1TR1042
    encoder         : Lavf58.27.103
  Duration: 00:05:18.04, start: 0.067000, bitrate: 1103 kb/s
    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 960x540, 999 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
    Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp, 93 kb/s
Output #0, mp4, to 'out.mp4':
  Metadata:
    major_brand     : qt  
    minor_version   : 512
    compatible_brands: qt  
    artist          : yinyuetai.com
    album           : Yinyuetai
    date            : 11/11/16 22:40:13
    comment         : Yinyuetai-1TR1042
    encoder         : Lavf58.27.103
    Stream #0:0: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 960x540, q=2-31, 999 kb/s, 30 fps, 30 tbr, 16k tbn, 1k tbc
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 93 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 9534 fps=0.0 q=-1.0 Lsize=   42800kB time=00:05:17.95 bitrate=1102.7kbits/s speed= 944x    
video:38771kB audio:3635kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.927725%
gensees-iMac:study gensee$ 

从日志看出,音视频参数没有变

分离视频

ffmpeg -i out.flv -an -vcodec copy out.h264

-an 意思为audio no 不输出音频

gensees-iMac:study gensee$ ffmpeg -i out.flv -an -vcodec copy out.h264
ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.38)
  configuration: --prefix=/usr/local/ffmpeg --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265 --enable-filter=delogo --enable-debug --disable-optimizations --enable-libspeex --enable-videotoolbox --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=
  libavutil      56. 29.100 / 56. 29.100
  libavcodec     58. 53.100 / 58. 53.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 55.100 /  7. 55.100
  libswscale      5.  4.101 /  5.  4.101
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Input #0, flv, from 'out.flv':
  Metadata:
    major_brand     : qt  
    minor_version   : 512
    compatible_brands: qt  
    artist          : yinyuetai.com
    album           : Yinyuetai
    date            : 11/11/16 22:40:13
    comment         : Yinyuetai-1TR1042
    encoder         : Lavf58.27.103
  Duration: 00:05:18.04, start: 0.067000, bitrate: 1103 kb/s
    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 960x540, 999 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
    Stream #0:1: Audio: aac (LC), 48000 Hz, stereo, fltp, 93 kb/s
Output #0, h264, to 'out.h264':
  Metadata:
    major_brand     : qt  
    minor_version   : 512
    compatible_brands: qt  
    artist          : yinyuetai.com
    album           : Yinyuetai
    date            : 11/11/16 22:40:13
    comment         : Yinyuetai-1TR1042
    encoder         : Lavf58.27.103
    Stream #0:0: Video: h264 (Main), yuv420p(progressive), 960x540, q=2-31, 999 kb/s, 30 fps, 30 tbr, 30 tbn, 30 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame= 9534 fps=0.0 q=-1.0 Lsize=   38771kB time=00:05:17.73 bitrate= 999.6kbits/s speed= 768x    
video:38771kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000156%

分离音频

ffmpeg -i out.flv -vn -acodec copy out.mp3

-vn 意思为video no 不输出视频

猜你喜欢

转载自blog.csdn.net/shengpeng3344/article/details/93475551