🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝 🥰 博客首页:knighthood2001 😗 欢迎点赞👍评论🗨️ ❤️ 热爱python,期待与大家一同进步成长!!❤️ |
目录
之前内容(建议看一下)
这年头还不来尝试线稿图视频??https://blog.csdn.net/knighthood2001/article/details/126175764?spm=1001.2014.3001.5502线稿图视频制作--从此短视频平台不缺上传视频了https://blog.csdn.net/knighthood2001/article/details/126912547?spm=1001.2014.3001.5502
目前文件夹比较清晰,vedio文件夹中是经过线稿图转换前后的视频,data文件夹是代码运行过程中,线稿图制作过程中产生的相应的文件。
代码更改如下:
- lj = 'vedio/'
- file = 'bang.mp4'
- filename = file.split(".")[0]
- mp3_file = f'{filename}.mp3'
-
- if not os.path.exists('vedio'):
- os.mkdir('vedio')
lj表示路径(/别忘了),
cap = cv2.VideoCapture(lj + file)
- # 音频提取
- def extract_audio():
- my_clip = mp.VideoFileClip(lj + file)
- my_clip.audio.write_audiofile('data/' + mp3_file)
video_merge.write_videofile(lj + 'final_' + b + '.mp4')
读取视频、音频提取和最终视频合成的时候,需要多这个路径。
- import os
- def deldir(dir):
- if not os.path.exists(dir):
- print('{}(路径)无内容'.format(dir))
- return
- if os.path.isfile(dir):
- os.remove(dir)
- return
- for i in os.listdir(dir):
- t = os.path.join(dir, i)
- if os.path.isdir(t):
- deldir(t)
- else:
- os.unlink(t)
- os.removedirs(dir)
- print('{}已删除'.format(dir))
-
构建一个这样的函数,从而把不需要的文件删除,后续只需要调用即可
- deldir('data/gougou')
- deldir('data/gougou_newpc')
- deldir('data/silence_dindin.mp4')
- deldir('data/dindin.mp3')
- deldir('data/dindin')
- # 删除不用的图片和视频
- deldir(path)
- # deldir(new_picture_path)
- deldir(file_name)
- from moviepy.editor import *
-
- clip = VideoFileClip(r"final_gougou1.mp4")
- clipColorx = clip.fx(vfx.colorx, 0.5)
- clipColorx.write_videofile(r"gougou2.mp4")
首先传入视频,如何更改上面的参数(0.5可以换成其他的,当它小于1,表示调暗,大于1表示调亮),最后将亮度变化的视频保存即可。
- import moviepy.video.fx.lum_contrast
- from moviepy.editor import *
-
- # 对比度调整
- clip = VideoFileClip(r"vedio/gougou.mp4")
- a = moviepy.video.fx.lum_contrast.lum_contrast(clip, contrast=0.3, contrast_thr=127)
- a.write_videofile(r"gougou13.mp4")
我们可以调整contrast参数
- def lum_contrast(clip, lum = 0, contrast=0, contrast_thr=127):
- """ luminosity-contrast correction of a clip """
- """剪辑的亮度对比度校正"""
-
- def fl_image(im):
- im = 1.0*im # float conversion
- corrected = im + lum + contrast*(im-float(contrast_thr))
- corrected[corrected < 0] = 0
- corrected[corrected > 255] = 255
- return corrected.astype('uint8')
-
- return clip.fl_image(fl_image)
这是lum_consrast()函数代码。
注意:调整视频亮度、对比度不在代码里面
- import os
- import cv2
- from PIL import Image
- from PIL import ImageFilter
- import moviepy.editor as mp
-
- lj = 'vedio/'
- # TODO 更改文件名称
- file = '1.mp4'
- filename = file.split(".")[0]
- mp3_file = f'{filename}.mp3'
-
- if not os.path.exists('vedio'):
- os.mkdir('vedio')
- if not os.path.exists('data'):
- os.mkdir('data')
- path = os.path.join('data', filename)
- new_picture_path = os.path.join('data', filename + "_newpc")
- if not os.path.exists(path):
- os.mkdir(path)
- if not os.path.exists(new_picture_path):
- os.mkdir(new_picture_path)
- # 无声视频
- file_name = os.path.join('data', 'silence_' + file)
-
- cap = cv2.VideoCapture(lj + file)
- fps = int(cap.get(cv2.CAP_PROP_FPS))
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
- frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
- print('fps:', fps, '\n', 'width:', width, '\n', 'height:', height, '\n', 'frames:', frames)
-
- for i in range(0, frames):
- flag, frame = cap.read()
- filename = path + '/{}.jpg'.format(str(i))
- print(filename)
- cv2.imwrite(filename, frame)
-
- # 音频提取
- def extract_audio():
- my_clip = mp.VideoFileClip(lj + file)
- my_clip.audio.write_audiofile('data/' + mp3_file)
-
- # 原图-线稿图转换
- def convert():
- for i in range(0, frames):
- '''细节'''
- square = Image.open(path + "/{}.jpg".format(i))
- square1 = square.filter(ImageFilter.DETAIL)
- '''轮廓'''
- square2 = square1.filter(ImageFilter.CONTOUR)
- square2.save(new_picture_path + "/{}.jpg".format(i))
- print(new_picture_path + "/{}.jpg".format(i))
-
- # 原图-线稿图转换(边缘增强)
- def convert_edge():
- for i in range(0, frames):
- square = Image.open(path + "/{}.jpg".format(i))
- square1 = square.filter(ImageFilter.EDGE_ENHANCE)
- '''轮廓'''
- square2 = square1.filter(ImageFilter.CONTOUR)
- square2.save(new_picture_path + "/{}.jpg".format(i))
- print(new_picture_path + "/{}.jpg".format(i))
-
- # 生成无声视频
- def picture_merge():
- size = (width, height)
- videowrite = cv2.VideoWriter(file_name, -1, fps, size)
- for i in range(0, frames):
- img = cv2.imread(new_picture_path + "/{}.jpg".format(i))
- videowrite.write(img)
- videowrite.release()
- print('end!')
-
- # 最终合成有声视频
- def final_merge():
- video = mp.VideoFileClip(file_name)
- audio = mp.AudioFileClip('data/' + mp3_file)
- video_merge = video.set_audio(audio)
- # todo 最终生成的带音频的视频
- b = file.split(".")[0]
- video_merge.write_videofile(lj + 'final_' + b + '.mp4')
-
- # 删除不要文件
- def deldir(dir):
- if not os.path.exists(dir):
- print('{}(路径)无内容'.format(dir))
- return
- if os.path.isfile(dir):
- os.remove(dir)
- return
- for i in os.listdir(dir):
- t = os.path.join(dir, i)
- if os.path.isdir(t):
- deldir(t)
- else:
- os.unlink(t)
- os.removedirs(dir)
- print('{}已删除'.format(dir))
-
- if __name__ == '__main__':
- extract_audio()
- convert()
- # convert_edge()
- picture_merge()
- final_merge()
- # 删除不用的图片和视频
- # deldir(path)
- # # deldir(new_picture_path)
- # deldir(file_name)
线稿图视频制作到目前应该就算完结了。感兴趣的可以去试试。
笔者源代码也发了,希望能支持一下,给个关注!!