Py学习  »  Python

安费诺传感器应用介绍和Python添加视频字幕示例

安费诺传感器学堂 • 1 年前 • 202 次点击  

本文中,我们延续上次慕尼黑上海电子展的热度,两位销售经理通过视频的方式推介了我们在工业、医疗和新能源车等方面应用的若干款产品。时间有限,难以涵盖更多的产品和深入说明。

若您需要了解更多的产品和应用内容,可以直接联系我们。

另外,本文提供了如何给视频添加字幕的Python示例代码。本文中视频的字幕添加即通过下面的示例代码实现。


视频-1    安费诺传感器的工业和医疗应用

视频-2    安费诺传感器的新能源车应用

附:

一. Python为视频添加字幕
    添加字幕时,需要把要添加的文字内容事先准备好,和视频内容和时间段契合。读取原视频后,处理之后的视频会另起名存储。请按照实际的文件名称路径进行调整。
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
"""(1)pip install moviepy(2)下载并安装:ImageMagick-7.1.1-36-Q16-HDRI-x64-dll.exehttps://imagemagick.org/script/download.php#windows"""#=============================# 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")

下一篇里,我们会通过Python将两个视频进行拼接和加二维码。
再下一篇里,我们可能会聊一下PID控制器参数的自动优化的实现。

热线:400-620 8986

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/172857
 
202 次点击