python 电影中的多处理
multiprocessing in python moviepy
致力于在 moviepy 上同时将视频剪辑编写为视频文件,而无需等待该过程完成,
因此我将我的视频分成 5 秒的片段
n=0
p = 5
clip = mp.VideoFileClip(videofile).subclip(n, n+p)
然后我给视频加上字幕,
x = 0
text2 = 'hello'+str(x)
text[x] = TextClip(text2, font='Amiri-regular',color='white',fontsize=24).set_duration(p).set_start(0)
然后我对前五个剪辑反复这样做,在第五个剪辑中,我将剪辑写成视频文件
我想在后台继续写入时继续处理视频的其余部分,所以我使用多处理,在编辑@Roland Smith 建议的代码后,我使用:
if float.is_integer(float(x)/5.0) == True and x != 0:
text2 = concatenate(text.values())
textd = text2.on_color(size=(clip.w ,text2.h),color=(0,0,0), col_opacity=0.6).set_pos('bottom')
video3[n] = CompositeVideoClip([VideoFileClip(videofile).subclip(n,5+n), textd])
def audioclip(data):
outname = str(data)[-10:].strip('>') + '.mp4'
data.write_videofile(outname,fps=24, codec='libx264')
return outname
names = video3.values()
h = multiprocessing.Pool()
audiofiles = h.map(audioclip, names)
gc.collect()
n = n+p
x = x+1
我导入了
from moviepy.editor import *
import moviepy.editor as mp
import os
import multiprocessing
from multiprocessing import pool
但是,我得到这个错误:
Traceback (most recent call last):
File "p2 (copy).py", line 128, in <module>
audiofiles = h.map(audioclip, names)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
请帮忙
你的问题不是很清楚,但我想我会告诉你如何并行提取音频。
您提供的第一个代码片段应该重新编写成一个函数。
import moviepy.editor as mp
from multiprocessing import pool
def audioclip(input):
clip = mp.VideoFileClip(input).subclip(0,20)
outname = input[:-3] + 'mp3'
clip.audio.write_audiofile(outname)
return outname
此外,您还需要一个输入文件名列表。
然后使用 multiprocessing.Pool
对象的 map
方法,将上述功能应用于所有视频。
# You should probably take the names from the command line...
names = ['foo.mp4', 'bar.mp4', 'spam.mp4', 'eggs.mp4']
p = multiprocessing.Pool()
audiofiles = p.map(audioclip, names)
这将并行地从剪辑中提取音频,默认情况下使用与 CPU 拥有的内核一样多的工作进程。
编辑:请注意,如果您使用map
,则必须对可迭代项进行pickle 并将其发送到工作进程。为了防止这种情况使用大量资源,最好是例如将大文件的 name 发送给工作进程(以便它可以读取文件本身)而不是该文件的 contents。否则,这将很快成为流程中的瓶颈。如果多个工作人员都必须读取同一个文件,这可能看起来很浪费,但所有现代操作系统所做的文件系统缓存应该可以缓解这种情况。
致力于在 moviepy 上同时将视频剪辑编写为视频文件,而无需等待该过程完成,
因此我将我的视频分成 5 秒的片段
n=0
p = 5
clip = mp.VideoFileClip(videofile).subclip(n, n+p)
然后我给视频加上字幕,
x = 0
text2 = 'hello'+str(x)
text[x] = TextClip(text2, font='Amiri-regular',color='white',fontsize=24).set_duration(p).set_start(0)
然后我对前五个剪辑反复这样做,在第五个剪辑中,我将剪辑写成视频文件
我想在后台继续写入时继续处理视频的其余部分,所以我使用多处理,在编辑@Roland Smith 建议的代码后,我使用:
if float.is_integer(float(x)/5.0) == True and x != 0:
text2 = concatenate(text.values())
textd = text2.on_color(size=(clip.w ,text2.h),color=(0,0,0), col_opacity=0.6).set_pos('bottom')
video3[n] = CompositeVideoClip([VideoFileClip(videofile).subclip(n,5+n), textd])
def audioclip(data):
outname = str(data)[-10:].strip('>') + '.mp4'
data.write_videofile(outname,fps=24, codec='libx264')
return outname
names = video3.values()
h = multiprocessing.Pool()
audiofiles = h.map(audioclip, names)
gc.collect()
n = n+p
x = x+1
我导入了
from moviepy.editor import *
import moviepy.editor as mp
import os
import multiprocessing
from multiprocessing import pool
但是,我得到这个错误:
Traceback (most recent call last):
File "p2 (copy).py", line 128, in <module>
audiofiles = h.map(audioclip, names)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
请帮忙
你的问题不是很清楚,但我想我会告诉你如何并行提取音频。
您提供的第一个代码片段应该重新编写成一个函数。
import moviepy.editor as mp
from multiprocessing import pool
def audioclip(input):
clip = mp.VideoFileClip(input).subclip(0,20)
outname = input[:-3] + 'mp3'
clip.audio.write_audiofile(outname)
return outname
此外,您还需要一个输入文件名列表。
然后使用 multiprocessing.Pool
对象的 map
方法,将上述功能应用于所有视频。
# You should probably take the names from the command line...
names = ['foo.mp4', 'bar.mp4', 'spam.mp4', 'eggs.mp4']
p = multiprocessing.Pool()
audiofiles = p.map(audioclip, names)
这将并行地从剪辑中提取音频,默认情况下使用与 CPU 拥有的内核一样多的工作进程。
编辑:请注意,如果您使用map
,则必须对可迭代项进行pickle 并将其发送到工作进程。为了防止这种情况使用大量资源,最好是例如将大文件的 name 发送给工作进程(以便它可以读取文件本身)而不是该文件的 contents。否则,这将很快成为流程中的瓶颈。如果多个工作人员都必须读取同一个文件,这可能看起来很浪费,但所有现代操作系统所做的文件系统缓存应该可以缓解这种情况。