最近全网都在玩的一种bug型的视频去重搬运技术 侠客正式版,常青剪辑,星光汇等,技术原理都一样
直接上代码:
这种技术对于一些小的视频平台还是非常有用的。可以直接搬运视频。做到1:1搬运
def merge_magic_video(worker, video1_path, video2_path, output_video_folder_path, target_width, target_height, target_bitrate, audio_mode):
worker.print(f'正在加载 {video1_path}')
video1_clip = VideoFileClip(video1_path, target_resolution=(target_height, target_width))
worker.print(f'正在加载 {video2_path}')
video2_clip = VideoFileClip(video2_path, target_resolution=(target_height, target_width))
print(video1_clip.w, video2_clip.h)
worker.print('调整两个视频帧率为60')
video1_clip = video1_clip.set_fps(60)
video2_clip = video2_clip.set_fps(60)
min_duration = min(video1_clip.duration, video2_clip.duration)
print('min duration', min_duration)
cursor = 0
offset = 2
cache_file_list = []
if not os.path.exists(os.path.join(output_video_folder_path, 'cache')):
os.mkdir(os.path.join(output_video_folder_path, 'cache'))
while cursor < min_duration:
end_point = cursor + (offset if cursor + offset <= min_duration else min_duration - cursor)
video1_subclip = video1_clip.subclip(cursor, end_point)
video2_subclip = video2_clip.subclip(cursor, end_point)
new_video_frames = [item for pair in zip(video1_subclip.iter_frames(), video2_subclip.iter_frames()) for item in pair]
new_video_clip = ImageSequenceClip(new_video_frames, fps=120)
file_path = os.path.join(output_video_folder_path, 'cache', f'{cursor}.mp4')
worker.print(f'正在导出缓存 {file_path}')
new_video_clip.write_videofile(file_path, bitrate=f'{target_bitrate}M', codec='libx264', fps=120, logger=MyBarLogger(worker))
cache_file_list.append(file_path)
cursor += offset
new_video_clip.close()
del new_video_clip
gc.collect()
worker.print('拼接缓存视频...')
output_clips = []
for file_path in cache_file_list:
cache_video_clip = VideoFileClip(file_path)
output_clips.append(cache_video_clip)
left_audio = video1_clip.audio
right_audio = video2_clip.audio
left_audio = left_audio.subclip(0, min_duration)
right_audio = right_audio.subclip(0, min_duration)
def make_frame(t):
left_frame = left_audio.get_frame(t)
right_frame = right_audio.get_frame(t)
if left_frame.ndim == 1 and right_frame.ndim == 1:
return np.column_stack((left_frame, right_frame))
if left_frame.ndim == 2 and right_frame.ndim == 2:
left_frame[:, 1] = right_frame[:, 1]
return left_frame
if left_frame.ndim > right_frame.ndim:
return np.column_stack((left_frame[:, 0], right_frame))
return np.column_stack((right_frame[:, 0], left_frame))
if audio_mode == 0:
audio_clip = left_audio
elif audio_mode == 1:
audio_clip = right_audio
else:
audio_clip = AudioClip(make_frame, duration=min_duration)
worker.print('导出视频...')
final_video_name = combine_video_names(video1_path, video2_path)
final_video_clip = concatenate_videoclips(output_clips)
final_video_clip = final_video_clip.set_audio(audio_clip)
output_video_path = os.path.join(output_video_folder_path, final_video_name)
final_video_clip.write_videofile(output_video_path, bitrate=f'{target_bitrate}M', fps=120, logger=MyBarLogger(worker))
final_video_clip.close()
del final_video_clip
gc.collect()
for output_clip in output_clips:
output_clip.close()
del output_clip
gc.collect()
left_audio.close()
right_audio.close()
audio_clip.close()
del audio_clip
gc.collect()
video1_clip.close()
video2_clip.close()
worker.print('导出完成')
def combine_video_names(video1_path, video2_path):
video1_name = os.path.splitext(os.path.basename(video1_path))[0]
video2_name = os.path.splitext(os.path.basename(video2_path))[0]
new_name = video1_name + '+' + video2_name + '.mp4'
return new_name
# vx:siweiwoo