搞AndroidRTMP,github上下了几个开源库,有的能跑有的不能跑,问题很多,直接看源码看的云里雾里。。。。
先从基础开始。。。。
先搞 FFmpeg
官网 ffmpeg.org 开源github上竟然是镜像地址 github.com/FFmpeg/FFmpeg
直接下载 macos release bin evermeet.cx/ffmpeg/ffmpeg-4.3.1.zip
下载龟速,看看里面的命令
libavutil is a library containing functions for
simplifying programming, including random number generators, data
structures, mathematics routines, core multimedia utilities, and
much more.
libavcodec is a library containing decoders and encoders
for audio/video codecs.
libavformat is a library containing demuxers and muxers for
multimedia container formats.
libavdevice is a library containing input and output
devices for grabbing from and rendering to many common multimedia
input/output software frameworks, including Video4Linux,
Video4Linux2, VfW, and ALSA.
libavfilter is a library containing media filters.
libswscale is a library performing highly optimized image
scaling and color space/pixel format conversion operations.
libswresample is a library performing highly optimized
audio resampling, rematrixing and sample format conversion
operations.
libavutil是一个包含简化程序功能的库,其中包括随机数生成器,数据结构,数学例程,核心多媒体实用程序等。
libavcodec是一个库,其中包含音频/视频编解码器的解码器和编码器。
libavformat是一个包含用于多媒体容器格式的解复用器和复用器的库。
libavdevice是一个包含输入和输出设备的库,用于从许多常见的多媒体输入/输出软件框架(包括Video4Linux,Video4Linux2,VfW和ALSA)中获取和呈现。
libavfilter是一个包含媒体过滤器的库。
libswscale是一个执行高度优化的图像缩放和颜色空间/像素格式转换操作的库。
libswresample是一个执行高度优化的音频重采样,重矩阵化和样本格式转换操作的库。
解压后 竟然只有 一个ffmpeg文件。。。。
这就跟满怀期待的去医院结果 护士只花了几分钟时间挤几个粉刺就收了300块钱一样闹心。。。。
然后 又去网站翻了翻,原来每个lib都是封好的,每个命令是单独下载的,总共有四个命令
- ffmpeg : A command line tool to convert multimedia files
between formats; 用于在格式之间转换多媒体文件的命令行工具;
- ffplay: A simple media player based on SDL and the FFmpeg libraries;一个基于SDL和FFmpeg库的简单媒体播放器;
- ffprobe: A simple multimedia stream analyzer;一个简单的多媒体流分析器;
- ffserver: 竟然没介绍。。。。
此处应该有图1
截屏2020-12-11 下午4.08.47.png
命令介绍
SENRSL:test-ffmpeg senrsl$ ./ffmpeg --help
ffmpeg version 4.3.1-tessus https://evermeet.cx/ffmpeg/ Copyright (c) 2000-2020 the FFmpeg developers
built with Apple clang version 11.0.0 (clang-1100.0.33.17)
configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libmysofa --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvmaf --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-version3 --pkg-config-flags=--static --disable-ffplay
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol
See man ffmpeg for detailed description of the options.
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
Global options (affect whole program instead of just one file):
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-filter_threads number of non-complex filter threads
-filter_complex_threads number of threads for -filter_complex
-stats print progress report during encoding
-max_error_rate maximum error rate ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filters
Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
SENRSL:test-ffmpeg senrsl$
1,帧率
每秒绘制的次数, -r int
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -r 30 out30.mp4
2, 码率
码率,即 比特率,
-b:v 为 视频码率;
-b:a 为音频码率;
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -b:v 200k out200k.mp4
200k的视频 糊了
3,限制大小
-fs limit_size (output)
Set the file size limit, expressed in bytes. No further chunk of
bytes is written after the limit is exceeded. The size of the
output file is slightly more than the requested file size.
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-fs 4194304 output4194304.mp4
哪里是略大。。。。限制4M,输出4.7M,大了很多。。。。
4,更改分辨率
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -s
640x480 output640x480.mp4
原分辨率是 1080 × 1920 ,执行后变成了 270 × 480
虽然变形了,但是 分辨率也没输出想要的。。。。
5,移除音频
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -an -i VID_20201211_155754.mp4 out-an.mp4
6,裁剪
裁剪左侧三分之一
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf crop=iw/3:ih:0:0 out-crop3.mp4
尺寸 360 × 1920
裁剪中间三分之一
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf crop=iw/3:ih:iw/3:0 out-crop32.mp4
裁剪右侧三分之一
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf crop=iw/3:ih:iw/3*2:0 out-crop33.mp4
居中裁剪 400x400
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf crop=400:400 out-crop400x400.mp4
宽高 从中间开始,裁剪一半
尺寸 540 × 960
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf crop=iw/2:ih/2 out-crop-w0.5h0.5.mp4
7,填充,边框填充30粉色边框
尺寸1140 × 1980
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf pad=iw+60:ih+60:30:30:pink out-pad30.mp4
8,翻转
水平翻转,就是左右镜像
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf hflip output-hflip.mp4
垂直翻转
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf vflip output-vflip.mp4
9,旋转
-vf transpose=0/1/2/3
0: 逆时针90度垂直翻转
1:顺时针90度
2:逆时针90度
3:顺时针90度水平翻转
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf transpose=0 out-transpose0.mp4
10,模糊或锐化
luma半径5,亮度0.8,亮度阈值0
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf smartblur=5:0.8:0 out-smartblue.mp4
luma半径是啥?
锐化或模糊视频帧,使用unsharp滤波器
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf unsharp out-unsharp.mp4
使用 hqdn3d 降噪
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -vf hqdn3d out-hqdn3d.mp4
11,画中画,加图片水印
左上角加图片
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i
ic_launcher.png -filter_complex overlay out-overlay.mp4
右上角加图片
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i ic_launcher.png -filter_complex overlay=W-w out-overlay-Ww.mp4
左下角
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i
ic_launcher.png -filter_complex overlay=0:H-h out-overlay-0Hh.mp4
右下角
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i
ic_launcher.png -filter_complex overlay=W-w:H-h
out-overlay-WwHh.mp4
背景图宽高:W,H
前景图 宽高 : w,h
5秒钟后,在200,600位置添加logo
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -itsoffset 5 -i ic_launcher.png -filter_complex overlay=200:600 out-time5-200x600.mp4
13,画中画,加视频水印
创建一个计时器
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -f lavfi -i testsrc -vf
crop=61:52:224:94 -t 30 timer.ogg
计时器添加到右上角
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i
timer.ogg -filter_complex overlay=W-w: out-timer.mp4
14, 画中画,加文字水印
添加文字在左上角显示
SENRSL:test-ffmpeg senrsl$ ./ffplay -f lavfi -i color=c=white -vf drawtext=fontfile=华康少女字体.ttf:text=你好啊懆拟码
用了ffplay命令,会自己弹出来播放
截屏2020-12-11 下午5.47.08.png
这个是从下到上的滚动。。。。
SENRSL:test-ffmpeg senrsl$ ./ffplay -i VID_20201211_155754.mp4
-vf
drawtext="fontfile=华康少女字体.ttf:text='这是顶部滚动':x=w-t*50:fontcolor=darkorange:fontsize=100"
这个也是从下到上滚动,从文件中读取文字
SENRSL:test-ffmpeg senrsl$ ./ffplay -i VID_20201211_155754.mp4 -vf drawtext="fontfile=华康少女字体.ttf:textfile=word.txt:x=w-t*50:y=h-th:fontcolor=darkorange:fontsize=100"
也可以通过ffmpeg保存起来,除了命令不同 参数时一样的
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4
-vf
drawtext="fontfile=华康少女字体.ttf:textfile=word.txt:x=w-t*50:y=h-th:fontcolor=darkorange:fontsize=100"
out-drawtext-word.mp4
同样的命令,用ffplay和用ffmpeg输出效果竟然不同。。。。
ffplay看起来是方向错了
截屏2020-12-11 下午5.58.36.png
ffmpeg就是 预期的在底部滚动
截屏2020-12-11 下午6.02.04.png
15,拼接视频
垂直拼接vstack
水平拼接hstack
垂直拼接
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -i output-vflip.mp4 -filter_complex [0][1]vstack -y out-vstack.mp4
看起来之前android端调用的ffmpeg很多功能都没用用到啊,提供的这么多功能基本可以覆盖满足多数视频操作了。。。。
截屏2020-12-11 下午6.08.17.png
16,本地ffmpeg命令推流给srs server
之前用的开源srs server,里面虽然提供了本地推流方案,但是 文件是不全的。。。。
现在可以补全了
mp4转flv格式
SENRSL:test-ffmpeg senrsl$ ./ffmpeg -i VID_20201211_155754.mp4 -c:v libx264 -ar 22050 -crf 28 out.flv
本地推流
SENRSL:test-ffmpeg senrsl$ for((;;)); do \
> ./ffmpeg -re -i out.flv \
> -vcodec copy -acodec copy \
> -f flv -y rtmp://192.168.7.89/live/livestream; \
> sleep 1; \
> done
这样,就开始自动推流了,客户端也能播放了!!!
又是一周。。。。
2020年12月11日18:10:32
senRsl
2020年12月11日11:30:09
没有评论 :
发表评论