2020年12月9日 / 23次阅读 / Last Modified 2021年1月12日
图像处理
ffmpeg是个非常强大的处理多媒体数据的工具,为了让自己在往后的日子里能够轻松驾驭各种需求,专门为它开一篇博文,记录自己的使用总结。
ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.
ffmpeg reads from an arbitrary number of input "files" (which can be regular files, pipes, network streams, grabbing devices, etc.), specified by the "-i" option, and writes to an arbitrary number of output "files", which are specified by a plain output url. Anything found on the command line which cannot be interpreted as an option is considered to be an output url.
Each input or output url can, in principle, contain any number of streams of different types (video/audio/subtitle/attachment/data). The allowed number and/or types of streams may be limited by the container format. Selecting which streams from which inputs will go into which output is either done automatically or with the "-map" option (see the Stream selection chapter). To refer to input files in options, you must use their indices (0-based). E.g. the first input file is 0, the second is 1, etc. Similarly, streams within a file are referred to by their indices. E.g. "2:3" refers to the fourth stream in the third input file. Also see the Stream specifiers chapter.
As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times. Each occurrence is then applied to the next input or output file. Exceptions from this rule are the global options (e.g. verbosity level), which should be specified first.
Do not mix input and output files -- first specify all input files, then all output files. Also do not mix options which belong to different files. All options apply ONLY to the next input or output file and are reset between files.
这段文字反复读几遍,它规范了ffmpeg命令行使用。重点是:
ffprobe是跟ffmpeg配套的工具(还有一个是ffplay)。
$ ffprobe fun.mp4
# or
$ ffprobe -show_streams fun.mp4
这是ffmpeg的简要帮助信息,通过这个信息,我们可以查询更多的关于ffmpeg的配置和参数信息:
$ ffmpeg -h
...
Print help / information / capabilities:
-L show license
-h topic show help
-? topic show help
-help topic show help
--help topic show help
-version show version
-buildconf show build configuration
-formats show available formats
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methods
...
$ ffmpeg -h long # a little more details
$ ffmpeg -h full
$ ffmpeg -h muxer-flv
$ ffmpeg -h encoders=h264
比如,查看 -pix_fmts 支持的参数有哪些:
$ ffmpeg -pix_fmts
ffmpeg -i fun.mp4 -map 0:0 -ss 00:05:00 -t 00:00:02 v2s.mp4
-ss,指定截取开始时间;-t,指定时间长度;-map 0:0,选择第1个输入的0号stream(用ffprobe查看,0对应video,1对应audio)。
转成YUV原始视频流格式
ffmpeg -i v2s.mp4 -map 0:0 -c rawvideo -pix_fmt yuv420p v2s.420p.yuv
ffmpeg -i v2s.mp4 -c:v rawvideo -pix_fmt yuv420p v2s2.420p.yuv
-c,codec;-c:v,对应输入中默认的video;后面跟 rawvideo,表示转成原始YUV格式;-pix_fmt,像素采样及存储格式;最后是输出文件。以上两个命令等价,后一个使用了ffmpeg的默认选择流机制。(视频原始数据文件非常大,所以先截取2秒,然后再转,就这样一个420p.yuv文件都要64M)
将YUV转成H265格式
ffmpeg -pix_fmt yuv420p -s 1280x720 -i v2s.420p.yuv -c:v libx265 out.265
要指定输入文件的yuv格式和size(-s),输入这个地方有个坑,out.265这个后缀必须要是.265,否则ffmpeg不能判断输出文件格式。
播放YUV文件
ffplay -f rawvideo -video_size 1280x720 v2s.420p.yuv
播放yuv文件有点费劲,必须要指定 video_size,默认是0x0。
播放完后自动退出
ffplay -autoexit v2s.mp4
默认播放完后就停在最后一个画面。
指定播放尺寸
ffplay -x 320 -y 240 v2s.mp4
循环播放
ffplay -loop 5 v2s.mp4
如果 -loop 0,就一直循环。
播放界面快捷键:
暂停,重启:p,space
单帧模式:s
鼠标右键的作用:选择播放位置!根据点击位置计算进度百分比,并跳转到那个位置,此时cmd界面会显示 seek to N%.....
全屏:f,或者左键双击。
-- EOF --
本文链接:https://www.pynote.net/archives/3010
Ctrl+D 收藏本页
©Copyright 麦新杰 Since 2019 Python笔记