生活随笔
收集整理的這篇文章主要介紹了
把会议记录的照片和音频合成一个视频文件(python+ffmpeg)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前幾天故宮博物院的單霽翔老師來天津做講座,但是很遺憾講座名額有限,沒有機會親臨現場聆聽老先生的講演。
但是好在同事對現場錄音和拍照,看了一下錄音和照片的時間戳,大致可以合的上,因此萌生出把照片和錄音合并在一起的想法。
不說廢話,碼上開干。
起初的思路是用ffmpeg庫把照片按照關鍵幀的順序插進音頻中,合成完整的視頻。但是玩過視頻的人都知道,這么做是不可能成立的。
于是調整思路,先把照片時間戳找出來,構建字典,key是時間戳,value是照片名稱。然后就用到了傳說的opencv,按照音頻/照片序列時長,生成avi文件。當然,這時的avi文件很大,有一個多G,沒關系,后面用ffmpeg壓縮一下轉成了49m的mp4文件。
現在有了視頻文件,然后在ffmpeg中把錄音和這個合成的視頻文件合并一下就可以了。代碼如下:
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')imgtimestamp
= time
.strptime
(tags
['Image DateTime'].values
, '%Y:%m:%d %H:%M:%S')imgtimestamp
= time
.mktime
(imgtimestamp
)return imgtimestamp
def 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 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 dictbut 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 audioimgdict: keys:time, values:img nameimgdir: dir of images'''audiostart
= 1576736760audioend
= 1576745959imgstart
= min(imgdict
.keys
()) imgend
= max(imgdict
.keys
()) imgdir
= "../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_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
) invideodir
= "test.avi"inaudiodir
= "../audio/191219_001.mp3"combinevideo
(invideodir
, inaudiodir
) if __name__
== "__main__":main
()
總結
以上是生活随笔為你收集整理的把会议记录的照片和音频合成一个视频文件(python+ffmpeg)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。