无需重新编码视频即可连接图像和视频

无需重新编码视频即可连接图像和视频

为了将图像附加到视频并在输出视频中将它们拼接在一起,我想执行以下操作:

# To test this code, place it in a folder containing a 'demo.mkv' file and run it. 
image="image.png"
video="demo.mkv"

# Get video properties
width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$video")
height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$video")
codec=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$video")
fps=$(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 "$video")
audio_codec=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$video")
audio_sample_rate=$(ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$video")
audio_channels=$(ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$video")

# Generate example image
convert -size "${width}X${height}" xc:black -font Arial -pointsize 48 -fill white -gravity center -annotate +0+0 "TEXT" "$image"

# Generate the video using FFmpeg with the properties of the input video
video_from_image=$(basename "$image")
video_from_image="${video_from_image%.*}"
ffmpeg -loop 1 -framerate "$fps" -i "$image" -f lavfi -i anullsrc=r=$audio_sample_rate:cl=$audio_channels -t 4 -c:v "$codec" -c:a "$audio_codec" -vf "scale=$width:$height" "${video_from_image}.mkv"

# Concatenate image and video
echo "file '${video_from_image}.mkv'" >> list.txt
echo "file $video" >> list.txt
ffmpeg -f concat -safe 0 -i list.txt -c:v copy video.mkv

但是,尽管输出文件的大小和长度是两个输入文件的总和,输出却不显示输入视频的任何帧。它显示的唯一帧来自图像文件,其余的都是空白。

相关内容