日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/3/26 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 把会议记录的照片和音频合成一个视频文件(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, resizedef 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 1576736760def maketimedict(path):'''make a dict ,key is the imgtimestamp, value is the name of imagepath: 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:continuereturn resdef savejson(imgdict, outfile):'''save the imgdict as a json file'''with open(outfile, 'w') as fp:json.dump(imgdict, fp)return outfiledef loadjson(infile):'''load the json file as dictbut the key will turn to string'''with open(infile, 'r') as fp:res = json.load(fp)return resdef 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 outputfiledef makevideo(imgdict, imgdir):'''combine the images to video, no audioimgdict: keys:time, values:img nameimgdir: dir of images'''audiostart = 1576736760audioend = 1576745959imgstart = min(imgdict.keys()) # 1576737005imgend = max(imgdict.keys()) # 1576746028imgdir = "../images/"outdir = "./test.avi"count_frames = max([imgend, audioend]) - min([imgstart, audiostart]) + 1fps = 1size = (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]) + iim_name = imgdict.get(im_key)if im_name:inputstr = imgdir + im_nameprint(inputstr, 'time is ', im_key, i/count_frames*100, '%')frame = imread(inputstr)frame = resize(frame, size)videowriter.write(frame)videowriter.release()return outdirdef main():imgdir = "../images"imgdict = maketimedict(imgdir)savejson(imgdict, 'imgtime.json') # 存一下,以防萬一makevideo(imgdict, imgdir) # 生成中間文件,test.aviinvideodir = "test.avi"inaudiodir = "../audio/191219_001.mp3"combinevideo(invideodir, inaudiodir) # 生成最終合成文件, output.mp4if __name__ == "__main__":main()

總結

以上是生活随笔為你收集整理的把会议记录的照片和音频合成一个视频文件(python+ffmpeg)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。