用Python辅助你高效创作短视频
Crossin的编程教室
共 8104字,需浏览 17分钟
·
2021-05-25 12:46
大家好,欢迎来到 Crossin的编程教室 !
from moviepy.editor import *
def get_audio_from_video(video_path):
"""
从视频中提取音频
:param video:
:return:
"""
file_path = './source/' + gene_random() + '.wav'
video = VideoFileClip(video_path)
audio = video.audio
audio.write_audiofile(file_path)
return file_path
def handle_bgm(bgm_path, coefficient):
music = AudioFileClip(bgm_path)
# 音量调整为原来音量的倍数
out_music = music.fx(afx.volumex, coefficient).fx(afx.audio_fadein, 0.5).fx(
afx.audio_fadeout, 1)
# 保存背景音乐
out_music.write_audiofile('./source/output.wav')
使用 AU 做变调处理
修改调用百度云 API
使用 librosa 依赖库。
import librosa
source = "./source/source.wav"
# 加载背景音乐
y, sr = librosa.load(source)
# 修改
librosa.effects.pitch_shift(y, sr, n_steps=6)
y = shrinkstep(10, y, sr)
# 生成新的背景音乐文件
outputpath = "./source/result.wav"
librosa.output.write_wav(outputpath, y, sr)
分离 2 段视频的视频 Clip、音频 Clip
统一视频的分辨率
分别对视频的开头和结尾加入转场效果,比如淡入淡出效果
合并 2 段视频
合并 2 段音频
设置音频文件
保存视频文件
def transitions_animation(path_video1, path_video2):
"""
两段视频中转场动画(以淡入淡出为例)
注意:保证视频拍摄帧率一致
:param video1:
:param video2:
:return:
"""
# 获取视频时长
clip_video1 = VideoFileClip(path_video1)
duration_video1 = clip_video1.duration
# 获取视频音频
path_audio1 = get_audio_from_video(path_video1)
path_audio2 = get_audio_from_video(path_video2)
audio_video1 = AudioFileClip(path_audio1)
audio_video2 = AudioFileClip(path_audio2)
clip_video2 = VideoFileClip(path_video2)
duration_video2 = clip_video2.duration
print(f'两段视频的时长分别为:{duration_video1},{duration_video2}')
# 统一视频分辨率
w, h, fps = clip_video1.w, clip_video1.h, clip_video1.fps
clip_video2_new = clip_video2.resize((w, h))
# 转场时长,默认2s
transitions_time = 2
# 第一段视频执行淡出效果
subVideo1_part1 = clip_video1.subclip(0, duration_video1 - 2)
subVideo1_part2 = clip_video1.subclip(duration_video1 - 2).fadeout(2, (1, 1, 1))
# 第二段视频执行淡入效果
subVideo2_part1 = clip_video2_new.subclip(0, 3).fadein(3, (1, 1, 1))
subVideo2_part2 = clip_video2_new.subclip(3)
# 合并4段视频
result_video = concatenate_videoclips([subVideo1_part1, subVideo1_part2, subVideo2_part1, subVideo2_part2])
# 合并音频
result_audio = concatenate_audioclips([audio_video1, audio_video2])
# 视频设置音频文件
final_clip = result_video.set_audio(result_audio)
# pass 写入视频文件
def change_video_speed(video_path, speed, start, end):
"""
改变视频的速度
[MoviePy clip相关的重要api](https://juejin.im/post/5d1c4318f265da1ba9159912)
:param video_path:视频路径
:param speed:速度
:param start:开始时间
:param end:结束时间
:return:
"""
video = VideoFileClip(video_path)
# 速度变换
part1 = video.fl_time(lambda t: speed * t, apply_to=['mask', 'video', 'audio']).set_start(start).set_end(
end / speed)
# 余下时长恢复速度
part2 = video.subclip(end)
# 合成视频
result_video = concatenate_videoclips([part1, part2])
result_path = './source/result.mp4'
result_video.write_videofile(result_path)
def ghost_video(video_path, repeat_time, location):
"""
生成鬼畜视频
:param video_path: 视频路径
:param repeat_time: 重复次数
:param location: 处理位置
:return:
"""
video = VideoFileClip(video_path)
# 视频前部分
part1 = video.subclip(0, location)
# 视频目标部分,进行重复操作
target = video.subclip(location, location + 1)
# 视频后部分
part2 = video.subclip(location + repeat_time)
targets = []
# 分别加入 3 个部分
targets.append(part1)
for _ in range(repeat_time):
targets.append(target)
targets.append(part2)
# 合成、生成视频
pass
# 加入图片水印
# 包含:水印的时长、位置、透明度等
logo = (ImageClip("./source/logo.png")
.set_duration(video.duration)
.resize(height=50)
.margin(right=8, top=8, opacity=1)
.set_pos(("right", "top")))
final = CompositeVideoClip([video, logo])
# 加入文字字幕
clip = VideoFileClip(video_path)
# 字幕,文字内容、位置、展示时长
texpClip = TextClip(subtitle, fontsize=30, color='white').set_pos('bottom').set_duration(duration)
video = CompositeVideoClip([clip,texpClip.set_start(start)])
# 快速转码压缩
ffmpeg -i source.mov -qscale 0 output.mp4
作者:星安果
来源:AirPython
_往期文章推荐_
评论