ffmpeg [$1] {[$2] -i $3} ... {[$4] $5} ...
{[$2] -i $3} 可多个, 输出部分 {[$4] $5} 也可多个-c: 指定编码器-c copy:直接复制原编码,不重新编码(这样比较快)-c:v:指定视频编码器-c:a:指定音频编码器-i: 指定输入文件-an: 去除音频流-vn: 去除视频流-preset:指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow。-y: 不经过确认,输出时直接覆盖同名文件。ffmpeg -formatsffmpeg -codecsffmpeg -encodersffmpeg -i video.mp4ffmpeg -hide_banner -i video.mp4ffmpeg -i input.mp4 -vn -c:a copy output.aac
ffmpeg -i input.aac -i input.mp4 output.mp4
ffmpeg -i input.mp4 -c:v rawvideo -pix_fmt yuv420p output.yuv
ffmpeg -i 1.jpg -filter_complex color=s=1280x720:c=black[vbg];[0:v]scale=1280x720[sv];[vbg][sv]overlay[vout] -map [vout] -ss 0 -to 10 -y 1.mp4
ffmpeg -f lavfi -i testsrc=duration=100:size=1280x720:rate=30:decimals=2 -pix_fmt yuv420p -vcodec libx264 output.mp4
生成空画面测试视频:
ffmpeg -f lavfi -i color=c=blue:s=1280x720:r=30 -pix_fmt yuv420p ...
详细看 ffmpeg 滤镜文档ffmpeg-filters。如果是实时视频流,比如往v4l2推流,加 -re 参数来以视频原始速度来生成:
ffmpeg -re -f lavfi -i color=c=blue:s=1280x720:r=30 -pix_fmt yuv420p -f v4l2 /dev/video0
ffmpeg -i input.mp4 -profile:v baseline -level 3.0 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls .\outputDir\index.m3u8
ffmpeg -i input.mp4 -f null /dev/null
ffmpeg -re -stream_loop -1 -i input.mp4 -vcodec copy -vbsf h264_mp4toannexb -f rtsp -rtsp_transport tcp rtsp://192.168.0.165:8554/chn_name
ffmpeg -i input704x576.mp4 -vf "scale=384:216:force_original_aspect_ratio=decrease,pad=384:216:-1:-1:color=green" output.mp4
ffplay -v debug -x 640 -y 380 https://192.168.0.151:6161/dev0.flv -fflags nobuffer -analyzeduration 1000000
ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -f null -
注意,此条命令只测试硬解效率,实际硬解时时间主要耗费在从GPU拷贝数据 av_hwframe_transfer_data 速率慢。详见
i965_dri_video.so 有点老不能支持比较新的intel cpu, 判断方法:lspci ,记住 VGA 该行的 id, 比如下面的 3e98 lspci | grep VGA 00:02.0 VGA compatible controller: Intel Corporation Device 3e98 (rev 02)
0x3e98, 如果有对应的id, 则可以编译对应新版本的 intel-vaapi-driver 来解决。如果没有,则大概需要使用 media-driver# 编译对应版本的libva sudo apt install autoconf libtool build-essential pkg-config git clone https://github.com/intel/libva.git cd libva git checkout 2.13.0 # 改为你需要的对应版本 ./autogen.sh --prefix=/opt/intel/libva --libdir=/opt/intel/libva/lib make sudo make install # 编译 intel-vaapi-driver git cone https://github.com/intel/intel-vaapi-driver.git cd intel-vaapi-driver git checkout 2.4.1 # 改为你需要的对应版本 export PKG_CONFIG_PATH=/opt/intel/libva/lib/pkgconfig ./autogen.sh make sudo make install # 设置库路径就可以使用新驱动来硬解 export LD_LIBRARY_PATH=/opt/intel/libva/lib:$LD_LIBRARY_PATH ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -f null -
decctx→pkt_timebase = stream→time_base, 可抛弃一些样本,优化部分延时问题。设置为无缓存 AVFMT_FLAG_NOBUFFER 也可解决部分延时:AVFormatContext* pFormatCtx = avformat_alloc_context(); pFormatCtx->flags = AVFMT_FLAG_NOBUFFER; int ret = avformat_open_input(&pFormatCtx, ...);
avformat_open_input 默认是阻塞的,如果是 tcp 连接,listen_timeout(单位秒) 或者 stimeout(单位微秒) 来达到超时处理pFormatCtx→interrupt_callbackavformat_find_stream_info 的返回时间和 pFormatCtx→probesize 与 pFormatCtx→max_analyze_duration 相关,哪个先达到就返回。avformat_find_stream_info 期间内遇不到关键帧, 则 pix_fmt、宽高等信息将为空, 需要等到实际解码循环时才能得到AVCodecContext 设置多线程才能利用多核CPU,这样1080P以上视频解码才不卡codec_ctx_->thread_count = av_cpu_count(); codec_ctx_->thread_type = FF_THREAD_FRAME;
avcodec_send_packet 与 avcodec_receive_frame 并不是一一对应的,两者是异步分开的frame→best_effort_timestamp 可能是更好的帧播放 timestamp.avcodec_send_packet时 packet参数 为 NULL,解码器进入 flush 模式,此时可循环 avcodec_receive_frame 获取缓存的frame, 直到返回 AVERROR_EOFavcodec_flush_buffers 可直接清除解码器缓存帧,用于 seek 操作或切换流操作。avformat_open_input 会自动 free 第四个非空参数 *options, 并把找不到的选项重新申请内存并返还给 *options,所有需要手动判断 *options 是否非空并释放, 否则内存泄露AVERROR 修饰,定义见 libavutil/error.hav_strerror 函数来得到文本描述字符串;参考 ffplay 代码:string AvStrError(int err) { char errbuf[128]; const char *errbuf_ptr = errbuf; if (av_strerror(err, errbuf, sizeof(errbuf)) < 0) { errbuf_ptr = strerror(AVUNERROR(err)); } return string(errbuf_ptr); }
av_log_set_callback函数来设置日志回调函数,自行输出各等级日志,方便查看具体信息。回调函数必须线程安全。