跳到主要内容

FFmpeg杂记

阅读量: 101阅读人次: 102

使用帮助

FFmpeg的帮助信息还是挺全的,结合 help 指令以及帮助文档应该能够释疑我们平常使用遇到的绝大部分问题。

FFmpeg 的 help 指令,能够输出很多有用的信息,输入 ffmpeg -h 能够看到以下输出:

......篇幅限制,内容仅截取部分......
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Getting help:
-h -- print basic options
See man ffmpeg for detailed description of the options.

Print help / information / capabilities:
-L show license
-h topic show help
--help topic show help
-pix_fmts show available pixel formats
-sample_fmts show available audio sample formats
-colors show available color names

Global options (affect whole program instead of just one file):
-loglevel loglevel set logging level
-v loglevel set logging level

Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)

Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)

例如我们想知道 FFmpeg 支持哪些像素格式, ffmpeg -pix_fmts 就能告诉我们。

UVC

ffmpeg播放USB摄像头

在Windows下,FFmpeg是通过DirectShow来打开USB摄像头的。

ffplay -f dshow video="Logi C270 HD WebCam"
ffmpeg -f dshow -i video="Logi C270 HD WebCam" camera.mp4

推流

ffmpeg -f dshow -i video="Logi C270 HD WebCam":audio="麦克风 (Logi C270 HD WebCam)" -vcodec libx264 -acodec libvo_aacenc -preset:v ultrafast -tune:v zerolatency -f rtsp rtmp://192.168.1.85:8554/test

常用命令

从音频文件(例如 mp3、flac)中提取 pcm :

ffmpeg -i demo.mp3 -f s16le -acodec pcm_s16le -ar 16000 -ac 1 output.pcm

将 16kHz、16bit、1通道的 pcm 音频文件转换为 48kHz、16bit、2通道的 pcm 音频文件:

ffmpeg -f s16le -ar 16000 -ac 1 -i input.pcm -f s16le -ar 48000 -ac 2 output.pcm

将 8kHz、16bit、1通道的 pcm 音频文件转换为 16kHz、16bit、1通道的 pcm 音频文件:

ffmpeg -f s16le -ar 8000 -ac 1 -i input.pcm -f s16le -ar 16000 -ac 1 output.pcm

ffmpeg将一个1通道mp3文件,转换为 2通道,16bit,16000采样率的pcm文件,但是左通道数据为静音:

# 左通道静音
ffmpeg -i 20200316_1900.mp3 -ac 2 -ar 16000 -filter_complex "[0:a]pan=stereo|c0=0*c0|c1=1*c0[a]" -map "[a]" -f s16le -acodec pcm_s16le 20200316_1900.pcm

# 右通道静音
ffmpeg -i 20200316_1900.mp3 -ac 2 -ar 16000 -filter_complex "[0:a]pan=stereo|c0=1*c0|c1=0*c0[a]" -map "[a]" -f s16le -acodec pcm_s16le 20200316_1900.pcm

从视频文件中提取 h264 裸流:

ffmpeg -i sample.mp4 -an -vcodec libx264 -preset slow -profile baseline video_stream.h264
  • -an:这个选项表示禁用音频(audio no)。它会从输出中移除所有音频流,只保留视频流。
  • -vcodec libx264:这个选项指定视频编码器(video codec)。libx264是一个开源的H.264/AVC视频编码库,被广泛用于高质量的视频压缩。这条命令指示FFmpeg使用libx264编码器来处理视频。
  • -preset slow:这个选项设置编码预设(encoding preset)。预设是一组预定义的编码参数,用于平衡编码速度和压缩质量。slow预设会使用更多的CPU时间进行编码,以获得更高的压缩率和更好的视频质量。
  • -profile baseline:这个选项设置H.264的profile。Profile定义了编码器支持的功能集。baseline profile是最基本的profile,具有广泛的兼容性,几乎所有H.264解码器都支持它。

重定向日志输出

使用av_log_set_callback()

void ffmpegLogCallback(void *avcl, int level, const char *fmt, va_list vl) {
if (level == AV_LOG_VERBOSE) return; // 日志太多不打印
char buffer[2048] = {0};
vsnprintf(buffer, sizeof(buffer), fmt, vl);
if ((level == AV_LOG_FATAL) || (level == AV_LOG_PANIC)) {
LOG_CAT(fatal, FFmpeg) << buffer;
} else if (level == AV_LOG_ERROR) {
LOG_CAT(error, FFmpeg) << buffer;
} else if (level == AV_LOG_WARNING) {
LOG_CAT(warning, FFmpeg) << buffer;
} else if (level == AV_LOG_INFO) {
LOG_CAT(info, FFmpeg) << buffer;
} else {
LOG_CAT(info, FFmpeg) << "[" << level << "] " << buffer;
}
}

av_log_set_callback(&ffmpegLogCallback);