使用FFMPEG以最小的重新编码添加覆盖 [英] Adding an Overlay Using FFMPEG With Minimal Re-Encoding

查看:108
本文介绍了使用FFMPEG以最小的重新编码添加覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

FFMPEG is really useful for cutting a part of a video, without re-encoding the video.

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

I know it is also possible to use FFMPEG for adding an Overlay Image to a video, in a certain part of the video (for example from 10secs till 20secs).

我的问题是:如果我对图像进行这种覆盖,是否会因此而对整个视频重新编码?还是只是相关的持续时间会被编码?

My question is: If I do this overlaying of an image, will the whole video get re-encoded because of that? Or just the relevant duration will be encoded?

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

Also are there any options that I can use to make the re-encoding minimal?
The purpose if of course to keep the quality of the video like the original one..
(I would ask for no re-encoding at all, but I don't see how that might be possible...)

谢谢

推荐答案

如果您将图像叠加在一部分上,则整个视频将被重新编码.您可以避免对整个内容进行重新编码的一种方法是剪切出您希望覆盖的部分,而仅对该片段进行重新编码(请参见-t duration开关和-ss position开关://ffmpeg.org/ffmpeg.html"rel =" nofollow noreferrer>文档

The whole video will get re-encoded if you overlay an image on part of it. One way you could avoid re-encoding the entire thing would be to clip out the portion you wish to overlay and only re-encode that piece (see the -t duration switch and the -ss position switch in the documentation

您将希望在整个过程中保持当前的编码参数.拆分时这很容易做到,因为您可以对编解码器开关使用copy参数,例如-c:a copy -c:v copy

You will want to maintain the current encoding parameters throughout the process. This is easy to do when splitting as you can use the copy parameter for the codec switch(es) such as -c:a copy -c:v copy

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

To conceptualize (note that these are not complete commands):

第1部分:电影的开头(不希望覆盖的前10秒)(通过ffmpeg -i SourceFileName -t 10 -c:a copy -c:v copy SourceFileNameP1.mkv获得,其中SourceFileName是要处理的视频. 第2部分:您要叠加的10至20秒之间的影片部分(通过ffmpeg -i SourceFileName -ss 10 -t 10 -c:a copy -c:v copy SourceFileNameP2获得) 第3部分:电影结尾(通过`ffmpeg -ss 20 -c:a复制-c:v复制获得)

Part1: Beginning of Movie (first 10 seconds which you do not wish to overlay) (obtained with ffmpeg -i SourceFileName -t 10 -c:a copy -c:v copy SourceFileNameP1.mkv where SourceFileName is your video to process. Part2: Part of movie between 10 and 20 seconds that you want to overlay (obtained with ffmpeg -i SourceFileName -ss 10 -t 10 -c:a copy -c:v copy SourceFileNameP2) Part3: End of movie (obtained with `ffmpeg -ss 20 -c:a copy -c:v copy)

奖金提示:通过将`-ss参数移动到输出文件之前,可以减慢切削速度,但可以实现更精确的切削.这样会从输出中丢弃帧,而不是在创建输出之前尝试在输入上寻找正确的位置.

Bonus tip: you can get slower but more exact cutting by moving the `-ss parameter to before the output file. This will drop frames from the output rather than attempting to seek to the correct position on the input prior to creating output.

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

If you don't know the encoding details of the source file, you can obtain them with ffprobe SourceFileName or my favorite mediainfo SourceFileName

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

I'm recommending using a Matroska container for at least the intermediate output due to it's flexibility and low overhead.

这是一个脚本,您可以使用它(在基于Debian的系统上)获取匹配的必要参数.

Here's a script you can use (on Debian based systems) to obtain the necessary parameters to match.

#!/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.

完成后,您就可以将所有部分都连接在一起.

Once this is complete you can join all the pieces together.

mkvmerge -o joined.mkv Part1 + Part2Reencoded + Part3

请注意,重新编码总是会导致质量下降,因此片段之间的连接可能会显示可见的缺陷.由于同时出现和消失叠加层引起的干扰,这可能会或可能不会引起注意.

Note that re-encoding always results in some quality loss so the joins between the pieces may show visible defects. This may or may not be noticeable with the distraction caused by the overlay appearing and disappearing at the same time codes.

根据材料的长度,这可能会大大减少您的重新编码时间,并且具有仅重新编码必须重新编码的附加好处.

This may reduce your re-encoding time significantly depending on the length of the material and has the added benefit of only re-encoding that which must be re-encoded.

此处中介绍了如何覆盖您的重新编码的段.然后您可以调整接受的答案以匹配您的材料.

How to overlay your re-encoded segment is covered here and you can adjust the accepted answer to match your material.

这篇关于使用FFMPEG以最小的重新编码添加覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆