前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《FFmpeg从入门到精通》读书笔记(一)

《FFmpeg从入门到精通》读书笔记(一)

作者头像
全栈程序员站长
发布2022-09-13 10:54:39
1.5K0
发布2022-09-13 10:54:39
举报

大家好,又见面了,我是你们的朋友全栈君。

写在前面

最近在读《FFmpeg从入门到精通》这本书,结合着雷神的博客,学习音视频的知识~ 在学习的过程中,也记录了一些摘要。因为是边看边记的,所以一些要点在看到后面的时候,需要反过来整理前面的。我用有道云笔记写的markdown没法加图片,所以就先把这部分发了出来。后续会针对内容和排版一步步的优化,如果你被这凌乱的内容辣到了眼睛,请谅解哈哈哈~

2019.06.18

第一章 FFmpeg简介 第二章 FFmpeg工具使用基础


简介

书中开头用几个实例操作,让读着对FFmpeg的操作有一个直观的认识。例如以下的几个方法:

纵向镜像反转一半
ffmpeg -i 1.jpg -vf "split [main][tmp]; [tmp] crop=iw:ih/2:0:0, vflip [flip];[main][flip] overlay=0:H/2" 2.jpg
在这里插入图片描述
在这里插入图片描述

上面操作的含义:将图片分为main和tmp两部分,截取tmp的上半部分,反转,合并时位置在下半部分

原图

在这里插入图片描述
在这里插入图片描述

效果图

在这里插入图片描述
在这里插入图片描述
ffmpeg -i input.mp4 output.avi  ->  ffmpeg -i input.mp4 -f avi out.dat

ffmpeg的主要工作流程

1 解封装 Demuxing 2 解码 Decoding 3 编码 Encoding 4 封装 Muxing

其中需要经过六个步骤

1 读取输入源 2 进行音视频的解封装 (调用libavformat中的接口实现) 3 解码每一帧音视频数据 (调用libavcodec中的接口实现) 3.5 转换参数 4 编码每一帧音视频数据(调用libavcodec中的接口实现) 5 进行音视频重新封装(调用libavformat中的接口实现) 6 输出到目标

除了ffmpeg(提供转码、转封装等功能),还有ffplay(负责播放相关)和ffprobe(多媒体分析器)

ffprobe -show_streams 1.mp4
在这里插入图片描述
在这里插入图片描述
ffmpeg常用命令
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
ffmpeg的查询
ffmpeg -decoders   (类似:ffmpeg --encoders)

Decoders: V… = Video A… = Audio S… = Subtitle .F… = Frame-level multithreading …S… = Slice-level multithreading …X… = Codec is experimental …B. = Supports draw_horiz_band …D = Supports direct rendering method 1 V…D     012v    Uncompressed 4:2:2 10-bit V…D     4xm      4X Movie V…D     8bps      QuickTime 8BPS video V…D     aasc      Autodesk RLE V…D     aic      Apple Intermediate Codec V…D     alias_pix      Alias/Wavefront PIX image V…D     amv      AMV Video V…D     anm      Deluxe Paint Animation V…D     ansi      ASCII/ANSI art VF…D   apng      APNG (Animated Portable Network Graphics) image

第一列包含6个字段,第一个字段用来表示此编码器为音频、视频还是字幕,第二个字段表示帧级别的多线程支持,第三个字段表示段级别的多线程,第四个字段表示该编码器为实验版本,第五个字段表示draw horiz band模式支持,第六个字段表示直接渲染模式支持 第二列是编码格式 第三列是编码格式的详细说明

ffmpeg -filters  (滤镜)

… acrossfade      AA->A      Cross fade two input audio streams. T… adelay         A->A      Delay one or more audio channels. … aecho         A->A      Add echoing to the audio. … aeval          A->A      Filter audio signal according to a specified expression. T… afade        A->A      Fade in/out input audio. … aformat        A->A      Convert the input audio to one of the specified formats. … ainterleave       N->A      Temporally interleave audio inputs. … allpass        A->A      Apply a two-pole all-pass filter. … amerge        N->A      Merge two or more audio streams into a single multi-channel stream. … amix         N->A      Audio mixing. … anull         A->A      Pass the source unchanged to the output. T… apad         A->A      Pad audio with silence. … aperms        A->A      Set permissions for the output audio frame. … aphaser       A->A      Add a phasing effect to the audio. … aresample      A->A      Resample audio data. … areverse        A->A      Reverse an audio clip. … aselect         A->N      Select audio frames to pass in output. … asendcmd       A->A      Send commands to filters. … asetnsamples     A->A      Set the number of samples for each output audio frames. … asetpts        A->A      Set PTS for the output audio frame. … asetrate        A->A      Change the sample rate without altering the data. … asettb        A->A      Set timebase for the audio output link. … ashowinfo      A->A      Show textual information for each audio frame. … asplit        A->N      Pass on the audio input to N audio outputs. … astats        A->A      Show time domain statistics about audio frames. … astreamsync    AA->AA      Copy two streams of audio data in a configurable order. … asyncts       A->A      Sync audio data to timestamps …C atempo      A->A      Adjust audio tempo. … atrim        A->A      Pick one continuous section from the input, drop the rest. … azmq         A->A      Receive commands through ZMQ and broker them to filters. … bandpass      A->A      Apply a two-pole Butterworth band-pass filter. … bandreject      A->A      Apply a two-pole Butterworth band-reject filter. … bass         A->A      Boost or cut lower frequencies. … biquad        A->A      Apply a biquad IIR filter with the given coefficients. … bs2b          A->A      Bauer stereo-to-binaural filter. … channelmap      A->A      Remap audio channels. … channelsplit      A->N      Split audio into per-channel streams. … chorus        A->A      Add a chorus effect to the audio. … compand      A->A      Compress or expand audio dynamic range. T… dcshift        A->A      Apply a DC shift to the audio. … dynaudnorm     A->A      Dynamic Audio Normalizer. … earwax        A->A      Widen the stereo image. … ebur128       A->N      EBU R128 scanner. … equalizer      A->A      Apply two-pole peaking equalization (EQ) filter. … flanger        A->A      Apply a flanging effect to the audio. … highpass      A->A      Apply a high-pass filter with 3dB point frequency. … join          N->A      Join multiple audio streams into multi-channel output. …C ladspa      N->A      Apply LADSPA effect. … lowpass      A->A      Apply a low-pass filter with 3dB point frequency. … pan          A->A      Remix channels with coefficients (panning). … replaygain      A->A      ReplayGain scanner. … resample      A->A      Audio resampling and conversion. … sidechaincompress   AA->A      Sidechain compressor. … silencedetect     A->A      Detect silence. … silenceremove    A->A      Remove silence. … treble        A->A      Boost or cut upper frequencies. T.C volume      A->A      Change input volume.

(首先请原谅我的弟弟排版,尽力了。。)

第一列总共有三个字段,第一个字段是时间轴支持,第二个字段是分片线程处理支持,第三个字段是命令支持 第二列是滤镜名 第三列是转换方式,如音频转音频,视频转视频,创建音频,创建视频等 第四列是滤镜作用说明

查看ffmpeg支持的具体某一种demuxer、muxer类型,可以通过ffmpeg -h查看该类型的详细参数
ffmpeg -h muxer=flv

Muxer flv [FLV (Flash Video)]:   Common extensions: flv.   Mime type: video/x-flv.   Default video codec: flv1.   Default audio codec: mp3.

ffmpeg的封装转换 (参数 书P37)
ffmpeg的基本转码原理

ffmpeg工具的主要用途为编码、解码、转码以及媒体格式转换。

ffmpeg -i 1.mp4 -vcodec mpeg4 -b:v 200k -r 15 -an output2.avi
在这里插入图片描述
在这里插入图片描述

以上命令中参数含义: 1.转封装格式从mp4转为avi 2.视频编码从h264转换为mpeg4格式 3.视频码率从原来的16278 kb/s转换为200 kb/s 4.视频帧率从原来的24.15 fps转换为15 fps 5.转码后的文件不包括音频(-an参数)

ffprobe常用命令

ffmpeg-多媒体处理工具 ffprobe-多媒体查看工具

ffprobe -show_packets 1.mp4
在这里插入图片描述
在这里插入图片描述
 ffprobe -show_format 1.mp4
在这里插入图片描述
在这里插入图片描述
ffprobe -show_frames 1.mp4
在这里插入图片描述
在这里插入图片描述

[FRAME] media_type=video 帧的类型 stream_index=0 帧所在的索引区域 key_frame=1 是否为关键帧 pkt_pts=0 Frame包的pts width=1080 帧显示的宽度 height=2248 帧显示的高度 pix_fmt=yuv420p 帧的图像色彩格式 pict_type=I 帧类型 [/FRAME]

ffprobe -show_streams 1.mp4
在这里插入图片描述
在这里插入图片描述

[STREAM] index=0 流所在的索引区域 codec_name=h264 编码名 codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 编码全名 profile=Baseline 编码的profile codec_type=video 编码类型 codec_time_base=1/180000 编码的时间戳计算基础单位 codec_tag_string=avc1 编码的标签数据 coded_width=1088 图像的宽度 coded_height=2256 has_b_frames=0 包含B帧的信息 pix_fmt=yuv420p 图像显示的色彩格式 r_frame_rate=145/6 实际帧率 avg_frame_rate=5000/207 平均帧率 time_base=1/90000 事件基数(用来进行timestamp计算) bit_rate=16278017 码率 max_bit_rate=N/A 最大码率 nb_frames=165 帧数 [/STREAM]

ffprobe默认使用key-value的格式输出,可以通过-of xml(ini,json,csv,flat)来进行相应的格式输出,例如:

ffprobe -of json -show_streams 1.mp4

-select_streams可以选择值馋看音频(a)、视频(v)、字幕(s)信息

ffprobe -show_frames -select_streams v -of json 1.mp4
ffplay

可以作为播放器,也可以作为很多音视频数据的图形化分析工具,通过ffplay可以看到视频图像的运动估计方向、音频数据的波形等

参数:

x  强制设置视频显示窗口的宽度 y s  设置视频显示的宽高 fs  强制全屏显示 an  屏蔽音频 vn   屏蔽视频 sn  屏蔽字幕 ss  根据设置的秒进行定位拖动 t   设置播放视频/音频的长度 bytes  设置定位拖动的策略,0为不可拖动,1为可拖动,-1为自动 nodisp  关闭图形化显示窗口 f    强制使用设置的格式进行解析 window_title   设置显示窗口的标题 af   设置音频的滤镜 codec  强制使用设置的codec进行解码 autorotate  自动旋转视频

可视化分析工具
ffplay -showmode 1 1.mp3

ffplay -vismv pf 1.mp4     查看B帧和P帧的预测信息

pf P帧向前运动估计显示 bf B帧向前运动估计显示 bb B帧向后运动估计显示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
播放器如何解码每个宏块
ffplay -debug vis_mb_type 1.mp4
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/153348.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在前面
  • 简介
    • ffmpeg常用命令
      • ffmpeg的封装转换 (参数 书P37)
        • ffprobe常用命令
          • ffplay
            • 可视化分析工具
              • 播放器如何解码每个宏块
          相关产品与服务
          多媒体处理
          多媒体处理(Multimedia Processing,MMP)是数据万象推出的音视频处理服务,集成音视频转码、极速高清、精彩集锦、超分辨率、数字水印等能力,满足传媒、文旅、电商等各行业多媒体处理需求。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档