简体   繁体   English

Ffmpeg:缩放并裁剪视频然后叠加图像?

[英]Ffmpeg: Scale+crop video then overlay image?

This is my first SO question. 这是我的第一个SO问题。 Help me help you help me: does this question need any clarification? 帮帮我帮您帮忙:这个问题需要澄清吗?

Goal: A script that makes Instagram-ready videos with audio, and a logo overlay. 目标:一个脚本,使带有音频的Instagram就绪视频和徽标叠加层。 The script takes in an audio and video source and combines them. 该脚本接收音频和视频源并将其组合。 Important: the logo should have a consistent position and size for each video. 重要提示:徽标应与每个视频的位置和大小保持一致。 This probably means that all output videos should have the same width x height. 这可能意味着所有输出视频应具有相同的宽度x高度。

Any alternate approaches are welcome! 欢迎任何其他方法!

The ffmpeg command I'm calling from python is below. 下面是我从python调用的ffmpeg命令。 I try to scale the video to 720:-2 (so auto-height), then crop a 500x500 square from the center. 我尝试将视频缩放到720:-2(即自动高度),然后从中心裁剪500x500正方形。 The choices of 720 and 500 are arbitrary; 720和500的选择是任意的; better approaches are welcome. 欢迎使用更好的方法。

ffmpeg -i video.mp4 -i logo.png -i audio.mp3 -filter_complex "[0:v]scale=720:-2,crop=500:500[bg];[bg][1:v] overlay=(W-w)/2:(H-h)/2" -pix_fmt yuv420p -map 0:v -map 2:a -shortest + output.mp4

This script errors on some videos. 此脚本在某些视频上出错。

[Parsed_crop_1 @ 0x7fcf96401f00] Invalid too big or non positive size for width '500' or height '500'
[Parsed_crop_1 @ 0x7fcf96401f00] Failed to configure input pad on Parsed_crop_1

I'm new to ffmpeg so please guide me to correct usage of filter_complex. 我是ffmpeg的新手,所以请指导我正确使用filter_complex。 Thank you! 谢谢!

It looks like not all videos are the same height and width. 似乎并非所有视频的高度和宽度都相同。 Im not positive because it's been a while but when using scaling to width or height if all the videos you use aren't uniform height and width things can go funky, like your telling ffmpeg to place something where it cant. 我不是很积极,因为已经有一段时间了,但是如果您使用的所有视频的高度和宽度不是统一的,那么使用缩放比例来调整宽度或高度时,事情可能会变得很时髦,例如您告诉ffmpeg将其放置在无法放置的地方。 Some videos will work when the math is right and when its wrong you'll get errors. 在数学正确和错误的情况下,某些视频将起作用,但会出现错误。

I would use ffprobe to get the dimensions of the video. 我将使用ffprobe来获取视频的尺寸。

import os
import json
import subprocess

def getVidInfo(videoPath):
    '''This function gets json data from ffprobe'''
    # print vPath
    if os.path.exists(videoPath):
        command = ['ffprobe', '-loglevel', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', videoPath]
        pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        out, err = pipe.communicate()
        if not err is None:
            print 'err = '+str(err)
        return json.loads(out)

## This was taken from an old python2.7 project so you might need to 
## get proper keys if these dont work.

vidJson = getVidInfo('pathToYourVideo')
vWidth = vidJson['streams'][0]['width']
vHeight = vidJson['streams'][0]['height']

Then do your math from the acquired video dimensions for ffmpeg call. 然后根据获取的ffmpeg调用的视频尺寸进行数学计算。 Any way that is where I would start. 任何以这种方式我都会从这里开始。

Use 采用

ffmpeg -i video.mp4 -i logo.png -i audio.mp3 -filter_complex "[0:v]scale=720:-2,crop=min(500\\,min(iw\\,ih)):min(500\\,min(iw\\,ih))[bg];[bg][1:v] overlay=(Ww)/2:(Hh)/2" -pix_fmt yuv420p -map 2:a -shortest output.mp4

The new crop args will make sure crop does not try to pick a size larger than the frame. 新的作物参数将确保作物不会尝试选择大于框架的尺寸。

The choice of scale and crop values depend upon the use case, and have to be decided by you. 比例和作物值的选择取决于用例,并且必须由您决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM