ffmpeg
XiaO / 2024-03-11
NIS Elements 渲染出的视频,无论在视频加载的时候还是在播放的时候,总感觉怪怪的。那种怪吧,有点说不清道不明,但就是觉得哪里不太对劲儿。譬如,在播放的时候,明明看到进度条已经跑完了,然而视频并没有播放结束。如果通过一些 GUI 转码软件尝试转码,得到的时长远远小于真实的时长。
mediainfo 查看音视频文件最为相关的技术参数和标签数据
brew install mediainfo
mediainfo path/to/inputfile
# 结果
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 132 MiB
Duration : 1 s 998 ms
Overall bit rate : 553 Mb/s
Frame rate : 60.241 FPS
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L5
Format settings : CABAC / 3 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference frames : 3 frames
Format settings, Slice count : 4 slices per frame
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 1 s 998 ms
Source duration : 19 s 920 ms
Bit rate : 53.1 Mb/s
Width : 3 090 pixels
Height : 1 146 pixels
Display aspect ratio : 2.696
Frame rate mode : Constant
Frame rate : 60.241 FPS
Original frame rate : 30.000 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.249
Stream size : 12.6 MiB (10%)
Source stream size : 132 MiB (100%)
mdhd_Duration : 1998
Codec configuration box : avcC
细看时长,便会发现,Duration : 1 s 998 ms
和 Source duration : 19 s 920 ms
显示的时长根本不一样。这种情况通常发生在编辑/转码过程中从较长的视频源中提取短片段时。编码器会保留原始源的持续时间元数据,但会输出一个新的指定持续时间的缩短视频文件。这就导致了加载和播放的时候,给人一种很怪异的感觉。
ffmpeg 全能的音视频转码工具
提取源文件
将所有需要处理的视频放入同一个文件夹source
中,从封装的视频文件中提取处源文件并存储到 source extracted
文件夹中。
for f in "/Users/urzone/Movies/source/"*.mp4; do ffmpeg -i "$f" -c:v copy -map 0:v:0 "/Users/urzone/Movies/source extracted/${f##*/}"; done
# `for f in "/Users/urzone/Movies/source/"*.mp4` loops through all files with the `.mp4` extension in the directory `/Users/urzone/Movies/source/`.
# `-c:v copy` instructs ffmpeg to copy the video stream from the input video without re-encoding.
# `${f##*/}` extracts the filename (with extension)
添加音频
为 source extracted
文件夹中的所有视频,添加音频并存储到一个新的文件夹 source audio
。
for f in "/Users/urzone/Movies/source extracted/"*.mp4; do ffmpeg -i "$f" -i "/Users/urzone/Movies/audio.m4a" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -shortest "/Users/urzone/Movies/source audio/${f##*/}"; done
视频压缩
将 source audio
文件夹中的所有视频,重新编码压缩并存储到一个新的文件夹 source compressed
。
for f in "/Users/urzone/Movies/source audio/"*.mp4; do ffmpeg -i "$f" -c:v libx264 -crf 22 -preset slow -c:a copy "/Users/urzone/Movies/source compressed/${f##*/}"; done
# The CRF scale ranges from 0 to 51, where 0 is lossless and higher values indicate lower quality.
# CRF values below 17/18 in x264 (FFmpeg) are visually lossless, with no significant visual benefit compared to CRF 17/18.
# Lower CRF values (0-16) are good for preserving detail and texture in videos, while higher values compress noise but may lead to a loss of detail.
添加 logo
for f in "/Users/urzone/Movies/source compressed/"*.mp4; do ffmpeg -i "$f" -i /Users/urzone/Movies/U-M_Logo-Hex.png -i /Users/urzone/Movies/sig1.png -filter_complex "[1:v]scale=150:-1 [scaled_overlay1]; [2:v]scale=300:-1 [scaled_overlay2]; [0:v][scaled_overlay1]overlay=10:10[overlay1]; [overlay1][scaled_overlay2]overlay=W-w-10:H-h-10" -c:a copy -movflags +faststart "/Users/urzone/Movies/source logo/${f##*/}"; done
# /Users/urzone/Movies/U-M_Logo-Hex.png 设置 logo1,
# [1:v]scale=150:-1 设置 logo1 横向分辨率
# /Users/urzone/Movies/sig1.png 设置 logo2
# [2:v]scale=300:-1 设置 logo2 横向分辨率