~~~

import subprocess

def video_douyin(video_path):

"""

生成抖音格式的视频

https://blog.csdn.net/weixin_28676289/article/details/112157079

https://blog.csdn.net/u010140427/article/details/128998215

:param video_path: 视频地址

:return:

"""

ffmpeg_path = "/usr/local/Cellar/ffmpeg/6.0-with-options/bin/ffmpeg"

# ffmpeg -y -i demo.mp4 -ss 00:01:00 -to 00:02:00 -vf

# "scale=720:-1,pad=720:1280:0:(1280-(ih/iw)*720)/2,

# drawtext=fontcolor=white:fontfile='C:WindowsFontsmsyh.ttc':fontsize=60:text='上方文字':x=(w-tw)/2:y=300,

# drawtext=fontcolor=white:fontfile='C:WindowsFontsmsyh.ttc':fontsize=60:text='下面文字:x=(w-tw)/2:y=(h-300)" out.mp4

top1_text = f"drawtext=fontcolor=white:fontsize=60:text='上方文字1':x=(w-tw)/2:y=100"

top2_text = f"drawtext=fontcolor=blue:fontsize=60:text='上方文字2':x=(w-tw)/2:y=300"

bottom_text = f"drawtext=fontcolor=red:fontsize=60:text='下面文字':x=(w-tw)/2:y=(h-350)"

fill_color = "black" # red, 其他区域填充颜色

cmd_list_1280 = [f"{ffmpeg_path}", "-y", "-i", video_path, "-vf",

f'"scale=720:-1,pad=720:1280:0:(1280-(ih/iw)*720)/2:{fill_color},{top1_text},{top2_text},{bottom_text}"',

"out.mp4"]

cmd_list_1920 = [f"{ffmpeg_path}", "-y", "-i", video_path, "-vf",

f'"scale=1280:-1,pad=1280:1920:0:(1920-(ih/iw)*1280)/2:{fill_color},{top1_text},{top2_text},{bottom_text}"',

"out.mp4"]

cmd = " ".join(cmd_list_1920)

print(cmd)

ffmpeg_code = subprocess.call(cmd, shell=True)

print(f"生成抖音形式视频,ffmpeg_code:{ffmpeg_code}")

if name == '__main__':

video_1280 = "/Users/cj/PycharmProjects/BaseCapability/files/test/1280.mp4"

video_1920 = "/Users/cj/PycharmProjects/BaseCapability/files/test/1920.mp4"

video_douyin(video_1280)

~~~