Python操作视频和音频

安装

1
2
3
pip install moviepy -i https://mirrors.aliyun.com/pypi/simple/
pip install pandas -i https://mirrors.aliyun.com/pypi/simple/
pip3 install pydub -i https://mirrors.aliyun.com/pypi/simple/
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
import threading
import time

from moviepy.editor import *

# 定义最大允许的线程数量
max_threads = 5
# 创建 Semaphore 对象
semaphore = threading.Semaphore(max_threads)


def asyncConcat(startPath, endPath, middlePath, middleAudioPath, outputPath, semaphore):
with semaphore:
concat(startPath, endPath, middlePath, middleAudioPath, outputPath)


def concat(startPath, endPath, middlePath, middleAudioPath, outputPath):
try:
start = VideoFileClip(startPath)
end = VideoFileClip(endPath)
middleVideo = VideoFileClip(middlePath)
middleAudio = AudioFileClip(middleAudioPath)
# 正文持续时间
print('持续时间=', middleVideo.duration)
# 音频长度剪切
middleAudio = middleAudio.set_duration(middleVideo.duration)
# 替换音频
middleVideo = middleVideo.set_audio(middleAudio)
# 分辨率
resolution = middleVideo.size
width, height = resolution
print('分辨率=', resolution)
print('分辨率:宽=', width, '高:', height)
# 等比例放大或缩小
start = start.resize(resolution)
end = end.resize(resolution)
# 上下
# resultClip = clips_array([[start], [middle], [end]])
# 左右
# resultClip = clips_array([[start, middle, end]])
# 前后
resultClip = concatenate_videoclips([start, middleVideo, end])
startTime = time.time()
basename = os.path.basename(middlePath)
basename = basename.replace('.', '-已剪辑.')
resultClip.write_videofile(outputPath + basename, threads=50)
print('剪辑使用时间:', time.time() - startTime)

except Exception as e:
print('计算视频失败:', e)


if __name__ == '__main__':
startPath = "D:\\00-自媒体计划\\AI-start.mp4"
endPath = "D:\\00-自媒体计划\\AI-end.mp4"
middleDir = "f:\\Videos\\4K Tokkit\\\dbxbqm22\\"
middlePath = "f:\\Videos\\4K Tokkit\\dbxbqm22\\7257911883674668289.mp4"
middleAudioPath = "D:\\00-自媒体计划\\AI-背景音乐.MP3"
outputPath = "f:\\Videos\\4K Tokkit\\test1\\"
if not outputPath.endswith('\\'):
outputPath = outputPath + '\\'
count = 1
fileCount = 0
startTime = time.time()
files = os.listdir(middleDir)
# 创建线程
threads = []
for file in files:
count = count + 1
basename = os.path.basename(file)
if '.mp4' not in basename:
continue
if '已剪辑' in basename:
continue
if os.path.exists(outputPath + basename.replace('.', '-已剪辑.')):
print('视频已剪辑=', middleDir + basename)
continue
print('待剪辑视频=', middleDir + basename)
fileCount = fileCount + 1
# concat(startPath, endPath, middleDir + basename, middleAudioPath, outputPath)
thread = threading.Thread(target=asyncConcat, args=(
startPath, endPath, middleDir + basename, middleAudioPath, outputPath, semaphore))
threads.append(thread)
thread.start()
# 等待所有线程执行结束
for thread in threads:
thread.join()
print('共处理视频个数:', fileCount, ' 耗时:', time.time() - startTime)

本文地址: https://github.com/maxzhao-it/blog/post/3756efb0/