from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
"""
(1)pip install moviepy
(2)下载并安装:ImageMagick-7.1.1-36-Q16-HDRI-x64-dll.exe
https://imagemagick.org/script/download.php
"""
#=============================
# MIT License
#
# Copyright (c) 2024 Amphenol Sensors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#=============================
# 加载视频文件
video = VideoFileClip("Path_to_your_source_media.mp4")
# 添加多个字幕片段的信息
# 医疗和工业类应用传感器
subtitles = [
{"text": "医疗和工业类应用传感器", "start": 10, "end": 17, "pos": ("center", "bottom")},
{"text": "(1)医疗器械里的传感器: 主推我们的压力和温度传感器", "start": 17, "end": 24, "pos": ("center", "bottom")},
{"text": "具体的应用: 体外诊断设备,生化设备和麻醉机,呼吸机的压力温度测量", "start": 24, "end": 37, "pos": ("center", "bottom")},
{"text": "医疗手术耗材应用: 体内导管压力和温度传感器", "start": 37, "end": 49, "pos": ("center", "bottom")},
{"text": "医疗手术体内传感器特点:纤细如发,可入血管或者人体自然腔道", "start": 49, "end": 65, "pos": ("center", "bottom")},
{"text": "比如:颅内压、血管压,泌尿压和消化道压力及温度", "start": 65, "end": 74, "pos": ("center", "bottom")},
{"text": "(2)工业应用的压力变送器", "start": 74, "end": 84, "pos": ("center", "bottom")},
{"text": "适用于工业生产自动化设备和过程控制中监测气体、液体等介质的压力测量", "start": 85, "end": 94, "pos": ("center", "bottom")},
{"text": "以及工程机械,道路设备等压力测量", "start": 94, "end": 102, "pos": ("center", "bottom")},
{"text": "(3)振动加速度传感器和便携仪表", "start": 102, "end": 109, "pos": ("center", "bottom")},
{"text": "适用于石油,风电,化工,工厂生产线设备的状态监测", "start": 109, "end": 118, "pos": ("center", "bottom")},
{"text": "监测预报甚至诊断对于重要设备健康状态举足轻重", "start": 118, "end": 128, "pos": ("center", "bottom")},
{"text": "多款符合本安要求,耐高温的振动传感器,以及便携检测设备", "start": 128, "end": 134, "pos": ("center", "bottom")},
{"text": "总有适合您需求的一款", "start": 134, "end": 137, "pos": ("center", "bottom")},
]
# 将上面的字幕文字内容替换为:新能源车应用传感器
subtitles = [
{"text": "新能源汽车应用中的几款传感器推介", "start": 5, "end": 16, "pos": ("center", "bottom")},
{"text": "(1)混动发动机上的EGR(废气再循环)温度传感器", "start": 16, "end": 22, "pos": ("center", "bottom")},
{"text": "2种产品:传统燃油车的高温EGR温度传感器,混动发动机专用的200C°的EGR温度传感器", "start": 22, "end": 36, "pos": ("center", "bottom")},
{"text": "(2)EGR(废气再循环)差压传感器", "start": 36, "
end": 40, "pos": ("center", "bottom")},
{"text": "应用于混动汽车和传统燃油汽车的发动机EGR", "start": 40, "end": 47, "pos": ("center", "bottom")},
{"text": "(3)电机绕组温度传感器", "start": 47, "end": 52, "pos": ("center", "bottom")},
{"text": "(4)冷却液温度传感器", "start": 52, "end": 56, "pos": ("center", "bottom")},
{"text": "(5)全新的适用于高压800V~1200V充电桩/站的热电堆温度传感器", "start": 56, "end": 64, "pos": ("center", "bottom")},
{"text": "(6)热管理系统中的传感器", "start": 64, "end": 69, "pos": ("center", "bottom")},
{"text": "(6-1)冷媒温度传感器,全温+/-0.5C°误差,为新能源汽车节能增效", "start": 69, "end": 80, "pos": ("center", "bottom")},
{"text": "(6-2)P+T(压力+温度)传感器,同时也是当前R744(CO2)冷媒P+T传感器全球唯一量产的公司", "start": 80, "end": 91, "pos": ("center", "bottom")},
{"text": "(7)电池包漏液监测传感器,监测电池包电解液,冷却液管道泄漏", "start": 91, "end": 103, "pos": ("center", "bottom")},
{"text": "欢迎大家关注——安费诺传感器学堂", "start": 103, "end": 112, "pos": ("center", "bottom")},
]
# 创建字幕片段
font_path = r'C:/Windows/Fonts/msyhbd.ttc'
subtitle_clips = []
for subtitle in subtitles:
text = subtitle["text"]
start_time = subtitle["start"]
end_time = subtitle["end"]
position = subtitle["pos"]
# 创建字幕
txt_clip = TextClip(text, fontsize=24, color='blue1', font=font_path,method='caption', size=(video.w, None))
txt_clip = txt_clip.set_position(position).set_duration(end_time - start_time).set_start(start_time)
subtitle_clips.append(txt_clip)
# 将所有字幕片段与原视频合成
final_video = CompositeVideoClip([video, *subtitle_clips])
# 保存合成后的视频
final_video.write_videofile("Path_to_save_your_video_with_multiple_subtitles-1.mp4", codec="libx264", audio_codec="aac")