Code
import time
import cv2
def Video2Mp4(videoPath, outVideoPath):
capture = cv2.VideoCapture(videoPath)
fps = capture.get(cv2.CAP_PROP_FPS)
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
suc = capture.isOpened()
allFrame = []
while suc:
suc, frame = capture.read()
if suc:
allFrame.append(frame)
capture.release()
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
videoWriter = cv2.VideoWriter(outVideoPath, fourcc, fps, size)
for aFrame in allFrame:
videoWriter.write(aFrame)
videoWriter.release()
if __name__ == '__main__':
inputVideoPath = "mv_1639632186.3482149.avi"
outVideoPath = f"out_{int(time.time())}.mp4"
Video2Mp4(inputVideoPath, outVideoPath)
TS 转MP4
import glob
def ts_to_mp4(mp4Name, ts_path):
with open(mp4Name, 'wb') as fw:
files = glob.glob('{}/*.ts'.format(ts_path))
files.sort(key=lambda x: int(x.split('\\')[-1].split('.')[0]))
for file in files:
with open(file, 'rb') as fr:
fw.write(fr.read())
ts_to_mp4('result.mp4', 'tmp')