python视频转字符图再转视频

感谢各位博主的帮助!

import cv2 
vidcap = cv2.VideoCapture(r'视频路径.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
    success,image = vidcap.read()
    cv2.imwrite("pic/frame%d.jpg" % count, image)  # save frame as JPG file
    if cv2.waitKey(10) == 27:
        break
    count += 1
    # 视频转图片
def draw(pic):
    img = cv2.imread('pic/' + pic)
    img = img[:, :, (2, 1, 0)]

    blank = Image.new("RGB", [len(img[0]), len(img)], "white")
    drawObj = ImageDraw.Draw(blank)

    n = 5 # “极乐净土”字体大小

    font = ImageFont.truetype('C:/Windows/Fonts/Microsoft YaHei UI/msyhbd.ttc', size=n - 1)

    for i in range(0, len(img), n):
        for j in range(0, len(img[i]), n):
            text = '极乐净土'
            drawObj.ink = img[i][j][0] + img[i][j][1] * 256 + img[i][j][2] * 256 * 256
            drawObj.text([j, i], text[int(j / n) % len(text)], font=font)
            print('完成处理——', i, j)

    blank.save('new/new_' + pic, 'jpeg')


filelist = os.listdir('pic')
for file in filelist:
    draw(file)
# 转成的图片进行处理(颜色字体等)
import os
import cv2
import re
import subprocess


def resort(list):
    for i in range(len(list) - 1):
        for j in range(len(list) - 1):
            if int(re.findall(r'\d+', list[j])[0]) > int(re.findall(r'\d+', list[j + 1])[0]):
                list[j], list[j + 1] = list[j + 1], list[j]
    return list


def picvideo(path, size):
    # 文件路径
    filelist = os.listdir(path)  # 获取该目录下的所有文件名
    filelist = resort(filelist)

    '''
    fps:
    帧率:1秒钟有n张图片写进去
    '''
    fps = 24
    # size = (852,480) #图片的分辨率片
    file_path = 'video/new.avi'  # 导出路径
    fourcc = cv2.VideoWriter_fourcc('I','4','2','0')  # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式)

    video = cv2.VideoWriter(file_path, fourcc, fps, size)

    for item in filelist:
        if item.endswith('.jpg'):  # 判断图片后缀是否是.jpg
            item = path + '/' + item
            img = cv2.imread(item)  # 使用opencv读取图像,直接返回numpy.ndarray 对象,通道顺序为BGR ,注意是BGR,通道值默认范围0-255。
            video.write(img)  # 把图片写进视频

    video.release()  # 释放


picvideo(r'new', (852, 480)) # 分辨率与视频转图片的分辨率相同,转成视频


# 由原视频获取mp3音频文件
def video_mp3(file_name):
    outfile_name = file_name.split('.')[0] + '.mp3'
    subprocess.call('ffmpeg -i ' + file_name + ' -f mp3 ' + outfile_name, shell=True)
    print(outfile_name)


# 将音频和字符图视频合成文件
def video_add_mp3(file_name, mp3_file):
    outfile_name = file_name.split('.')[0] + '-txt.mp4'
    subprocess.call('ffmpeg -i ' + file_name + ' -i ' + mp3_file + ' -strict -2 -f mp4 ' + outfile_name, shell=True)


video_mp3(r'视频路径.mp4')
video_add_mp3('字符图转成视频的路径', r'音频的路径(outfile_name)'.mp3')
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读