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 = 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 = 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 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)
|