使用moviepy时遇到的错误

OSError: convert: error while loading shared libraries: libMagickCore-7.Q16HDRI.so.10: cannot open shared object file: No such file or directory

1
2
# 执行
ldconfig /usr/local/lib

使用moviepy组合SRT字幕文件和视频

有个视频需求,需要自动化的将字幕文件和视频组合在一起,这里选择的是moviepy,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

import re
from os.path import splitext, isfile

from moviepy.editor import (VideoFileClip,
TextClip,
CompositeVideoClip)


# 读取字幕文件
def read_srt(path):
content = ""
with open(path, 'r', encoding='UTF-8') as f:
content = f.read()
return content


# 字幕拆分
def get_sequences(content):
sequences = content.split('\n\n')
sequences = [sequence.split('\n') for sequence in sequences]
# 去除每一句空值
sequences = [list(filter(None, sequence)) for sequence in sequences]
# 去除整体空值
return list(filter(None, sequences))

# 转换时间
def strFloatTime(tempStr):
xx = tempStr.split(':')
hour = int(xx[0])
minute = int(xx[1])
second = int(xx[2].split(',')[0])
minsecond = int(xx[2].split(',')[1])
allTime = hour * 60 * 60 + minute * 60 + second + minsecond / 1000
return allTime

class RealizeAddSubtitles():
'''
合成字幕与视频
'''

def __init__(self, videoFile, txtFile):
self.src_video = videoFile
self.sentences = txtFile
if not (isfile(self.src_video) and self.src_video.endswith(('.avi', '.mp4')) and isfile(
self.sentences) and self.sentences.endswith('.srt')):
print('视频仅支持avi以及mp4,字幕仅支持srt格式')
else:
video = VideoFileClip(self.src_video)
# 获取视频的宽度和高度
w, h = video.w, video.h
# 所有字幕剪辑
txts = []
content = read_srt(self.sentences)
sequences = get_sequences(content)

# print(sequences)

# exit()

for line in sequences:
if len(line)<3:
continue
sentences = line[2]
start = line[1].split(' --> ')[0]
end = line[1].split(' --> ')[1]

start=strFloatTime(start)
end=strFloatTime(end)

start, end = map(float, (start, end))
span=end-start
# 超过24个字符则换行
if len(sentences) > 24:
zmstr = re.findall(r'.{24}', sentences)
zmstr.append(sentences[(len(zmstr) * 24):])
print(zmstr)
sentences = zmstr[0] + "\n" + zmstr[1]
# #ddddff
# https://moviepy-tburrows13.readthedocs.io/en/improve-docs/ref/VideoClip/TextClip.html

# stroke_color描边颜色,stroke_width 描边宽度, bg_color="red"
txt = (TextClip(sentences, fontsize=40,
font='SimHei', size=(w - 20, 80),
align='center', color='#FFF', stroke_color="black", stroke_width=2)
.set_position((10, h - 150))
.set_duration(span)
.set_start(start))

txts.append(txt)
# 合成视频,写入文件
video = CompositeVideoClip([video, *txts])
fn, ext = splitext(self.src_video)
video.write_videofile(f'{fn}_2带字幕{ext}')


if __name__ == '__main__':
'''调用方法示例'''
srt_path = 'go2.srt'
addSubtitles = RealizeAddSubtitles('go2.mp4', srt_path)

Python中的opencv图片处理

安装

  • conda install opencv-python

如果发生错误:PackagesNotFoundError: The following packages are not available from current channels: - opencv-python

- anaconda show conda-forge/opencv-python
- conda install --channel https://conda.anaconda.org/conda-forge opencv 
1
2
import numpy as np
import cv2
阅读更多

Python中的字符串的基本使用

字符串

  • 字符串的表示方式
  • 字符串的下标和切片
  • 字符串常见操作
  • 字符集和编码
  • 成员运算符
  • 字符串的format方法
1
2
3
4
5
a = "I'm Tom"  # 一对双引号 
b = 'Tom said:"I am Tom"' # 一对单引号
c = 'Tom said:"I\'m Tom"' # 转义字符
d = '''Tom said:"I'm Tom"''' # 三个单引号
e = """Tom said:"I'm Tom" """ # 三个双引号
阅读更多

numpy练习

1
import numpy as np
1
np.__version__
'1.19.2'
1
import pandas as pd
1
pd.__version__
'1.1.3'
1
import requests as requests
1
requests.__version__
'2.24.0'
阅读更多

numpy 的20道练习题

numpy 的20道练习题

1、创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1

1
2
3
4
import numpy as np
a1 = np.full((10,),fill_value=0)
a1[4] = 1
a1
array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])
阅读更多

Python爬虫-requests库

requests库【重点】

requests库也是一个网络请求库, 基于urllib和urllib3封装的便捷使用的网络请求库。

阅读更多