视频 – 使用具有最小重新编码的FFMPEG添加叠加

FFMPEG对于剪切视频的一部分非常有用,无需重新编码视频.

我知道也可以使用FFMPEG在视频的某个部分(例如从10秒到20秒)为视频添加叠加图像.

我的问题是:如果我这样做覆盖图像,整个视频会因此被重新编码吗?或者只是相关的持续时间会被编码?

还有什么选项可以用来使重新编码最小化?
当然,目的是保持视频的质量,如原始的..
(我会要求根本没有重新编码,但我不知道这可能是怎么回事……)

谢谢

最佳答案 如果您在部分图像上叠加图像,整个视频将被重新编码.一种可以避免重新编码整个事物的方法是剪切出你希望覆盖的部分,只重新编码该部分(参见 documentation中的-t持续时间开关和-ss位置开关)

您将需要在整个过程中维护当前的编码参数.拆分时很容易,因为可以使用编解码器开关的复制参数,例如-c:copy -c:v copy

概念化(注意这些不是完整的命令):

第1部分:电影的开始(你不希望重叠的前10秒)(使用ffmpeg获取-i SourceFileName -t 10 -c:复制-c:v复制SourceFileNameP1.mkv,其中SourceFileName是您要处理的视频.
第2部分:要覆盖10到20秒之间的电影部分(使用ffmpeg获取-i SourceFileName -ss 10 -t 10 -c:副本-c:v copy SourceFileNameP2)
第3部分:电影结束(使用`ffmpeg -ss 20 -c:copy -c:v copy获得)

额外提示:通过将`-ss参数移动到输出文件之前,您可以获得更慢但更精确的切割.这将从输出中丢弃帧,而不是在创建输出之前尝试在输入上寻找正确的位置.

如果您不知道源文件的编码详细信息,可以使用ffprobe SourceFileName或我最喜欢的mediainfo SourceFileName获取它们.

由于其灵活性和低开销,我建议至少使用Matroska容器进行中间输出.

这是您可以使用的脚本(在基于Debian的系统上)以获取匹配所需的参数.

#!/bin/bash
#mknfo.sh
#Written by Elder Geek for the Stack Exchange network
# 1/1/2018 
####################################################################################################
#bash script to create an nfo file which includes information to help joining video clips          #
####################################################################################################
# This function will create an nfo file including the tech specs for a specified media file        #
####################################################################################################
function shortinfo {
   echo $@
      mediainfo --Inform="General;Duration=%Duration/String2%\nFile size=%FileSize/String1%\nBit Rate=%OverallBitRate/String% " "$@"
   echo -n "Video: "
   mediainfo --Inform="Video;FrameRate=%FrameRate/String% BitRate=%BitRate/String% Resolution=%Width%x%Height% Codec=%CodecID%" "$@";
    echo -n "Audio: "
   mediainfo --Inform="Audio;Mode=%BitRate_Mode/String% BitRate=%BitRate/String% Format=%Format%" "$@";
   echo "-----------------------------------------------------------------------------"
}
####################################################################################################
# This function will check for the existence of mediainfo and attempt installation if not found     #
####################################################################################################
function getmi {
   echo "mediainfo is required and not found. Attempt install Y/N"
   read -n 1 solve
    if [ $solve=={Yy} ]
    then sudo apt-get -y install mediainfo
    else echo "Cannot continue"
    exit 1
    fi
}
####################################################################################################
# Main program                                             #
####################################################################################################
if [ $# -ne 1 ] 
    then    
    echo Error 
    echo "$0" requires a single filename argument. Example: "$0" Videofile
    exit 2
fi
exist=$(which mediainfo)
    if [ "$exist" == "" ];
    then getmi
    fi
target=$(pwd)"/"$1".nfo"
    if [ -e $target ] 
    then 
    echo Error: "$1.nfo" already exists
    exit 3
    fi
echo "Creating $target"
        shortinfo "$1" > "$target"
    exit 0


Now you'll want to re-encode the overlay section (Part2) of the video to exactly match the parameters (same audio and video codecs and same bitrate and sample rate as the original of Part1 and Part3 to allow for joining.

完成后,您可以将所有部分组合在一起.

mkvmerge -o joined.mkv Part1 Part2Reencoded Part3

请注意,重新编码总是会导致一些质量损失,因此这些部分之间的连接可能会显示出明显的缺陷.由于叠加在同一时间码上出现和消失而引起的分心,这可能会或可能不会引人注意.

这可能会显着缩短您的重新编码时间,具体取决于材料的长度,并且还具有仅重新编码必须重新编码的优点.

如何覆盖重新编码的细分受到保留here,您可以调整接受的答案以匹配您的资料.

点赞