把会议记录的照片和音频合成一个视频文件(python+ffmpeg)

前几天故宫博物院的单霁翔老师来天津做讲座,但是很遗憾讲座名额有限,没有机会亲临现场聆听老先生的讲演。
但是好在同事对现场录音和拍照,看了一下录音和照片的时间戳,大致可以合的上,因此萌生出把照片和录音合并在一起的想法。
不说废话,码上开干。
起初的思路是用ffmpeg库把照片按照关键帧的顺序插进音频中,合成完整的视频。但是玩过视频的人都知道,这么做是不可能成立的。
于是调整思路,先把照片时间戳找出来,构建字典,key是时间戳,value是照片名称。然后就用到了传说的opencv,按照音频/照片序列时长,生成avi文件。当然,这时的avi文件很大,有一个多G,没关系,后面用ffmpeg压缩一下转成了49m的mp4文件。
现在有了视频文件,然后在ffmpeg中把录音和这个合成的视频文件合并一下就可以了。代码如下:

# -*- coding: utf-8 -*-
import exifread
import os
import json
import time
import subprocess
import cv2
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize


def getexiftime(imgname):
    '''get the exif info of image
    '''
    with open(imgname, 'rb') as f:
        tags = exifread.process_file(f, stop_tag='Image DateTime')
    # tags['Image DateTime'].values = "2019:12:19 14:26:00"
    imgtimestamp = time.strptime(
        tags['Image DateTime'].values, '%Y:%m:%d %H:%M:%S')
    imgtimestamp = time.mktime(imgtimestamp)
    return imgtimestamp  # like 1576736760


def maketimedict(path):
    '''make a dict ,key is the imgtimestamp, value is the name of image
    path: dir of images, string
    '''
    res = dict()
    for item in os.listdir(path):
        imgpath = f'{path}/{item}'
        if os.path.isfile(imgpath):
            try:
                imgtimestamp = getexiftime(imgpath)
                key = int(float(imgtimestamp))
                res.setdefault(key, item)
            except Exception:
                continue
    return res


def savejson(imgdict, outfile):
    '''save the imgdict as a json file
    '''
    with open(outfile, 'w') as fp:
        json.dump(imgdict, fp)
    return outfile


def loadjson(infile):
    '''load the json file as dict
    but the key will turn to string
    '''
    with open(infile, 'r') as fp:
        res = json.load(fp)
    return res


def combinevideo(invideodir, inaudiodir):
    outputfile = "output.mp4"
    command1 = f"ffmpeg -i {invideodir} test.mp4"
    # 压缩视频文件
    subprocess.call(command1)
    command2 = f"ffmpeg -i test.mp4 -i {inaudiodir} -c:v copy -c:a aac -strict experimental {outputfile}"
    # 合并视频音频
    subprocess.call(command2)
    return outputfile


def makevideo(imgdict, imgdir):
    '''combine the images to video, no audio
    imgdict: keys:time, values:img name
    imgdir: dir of images'''
    audiostart = 1576736760
    audioend = 1576745959
    imgstart = min(imgdict.keys())  # 1576737005
    imgend = max(imgdict.keys())  # 1576746028
    imgdir = "../images/"
    outdir = "./test.avi"
    count_frames = max([imgend, audioend]) - min([imgstart, audiostart]) + 1
    fps = 1
    size = (640, 480)
    fourcc = VideoWriter_fourcc(*"MJPG")
    videowriter = cv2.VideoWriter(outdir, fourcc, fps, size)
    inputstr = imgdir + imgdict[imgstart]
    for i in range(count_frames):
        im_key = min([imgstart, audiostart]) + i
        im_name = imgdict.get(im_key)
        if im_name:
            inputstr = imgdir + im_name
        print(inputstr, 'time is ', im_key, i/count_frames*100, '%')
        frame = imread(inputstr)
        frame = resize(frame, size)
        videowriter.write(frame)
    videowriter.release()
    return outdir


def main():
    imgdir = "../images"
    imgdict = maketimedict(imgdir)
    savejson(imgdict, 'imgtime.json')  # 存一下,以防万一
    makevideo(imgdict, imgdir)  # 生成中间文件,test.avi
    invideodir = "test.avi"
    inaudiodir = "../audio/191219_001.mp3"
    combinevideo(invideodir, inaudiodir)  # 生成最终合成文件, output.mp4


if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值