新風(fēng)格網(wǎng)站灰色詞排名代做
目錄
視頻較長(zhǎng),分辨率較大,這個(gè)效果很好,不耗用內(nèi)存 ffmpeg
imageio,適合視頻較短
視頻較長(zhǎng),分辨率較大,這個(gè)效果很好,不耗用內(nèi)存 ffmpeg
import subprocess
import glob
import os
from natsort import natsorted
base_dir = r'C:\Users\Administrator\Videos\shuiyin\result'
output_file = r'output_shuiyin.mp4'
video_paths = glob.glob(base_dir + '/*.mp4')
video_paths = natsorted(video_paths)
with open('file_list.txt', 'w') as f:for file in video_paths:f.write(f"file '{file}'\n")
ffmpeg_command = ['ffmpeg','-f', 'concat', # 指定拼接模式'-safe', '0', # 允許絕對(duì)路徑'-i', 'file_list.txt', # 輸入的文件列表'-c:v', 'libx264', # 使用 libx264 編碼器'-c:a', 'aac', # 使用 aac 編碼音頻'-strict', 'experimental',# 使用實(shí)驗(yàn)性編碼output_file # 輸出文件路徑
]
subprocess.run(ffmpeg_command, check=True)print(f"視頻拼接完成,輸出文件:{output_file}")
imageio,適合視頻較短
import glob
from natsort import natsorted
from moviepy.editor import VideoFileClip, concatenate_videoclips
import glob
import os.pathfrom natsort import natsorted
import cv2
import imageioif __name__ == '__main__':#內(nèi)存base_dir =r"C:\Users\Administrator\Videos\shuiyin\0127"base_dir =r'C:\Users\Administrator\Videos\shuiyin\result'output_path = "pinjie_shuiyin.mp4"video_paths =glob.glob(base_dir +'/*.mp4')video_paths=natsorted(video_paths)imgs=[]res = []for file in video_paths:cap_a = cv2.VideoCapture(file) # 打開視頻Bfps = cap_a.get(cv2.CAP_PROP_FPS)frame_count = 0print(file)while True:ret, frame_a = cap_a.read()if not ret:break # 如果沒有讀取到幀,則跳出循環(huán)res.append(cv2.cvtColor(frame_a, cv2.COLOR_BGR2RGB))frame_count += 1 # 釋放視頻資源cap_a.release()imageio.mimsave(output_path, res, "mp4", fps=fps, macro_block_size=None)