- OpenCV Python 教程
- OpenCV Python - 首頁
- OpenCV Python - 概述
- OpenCV Python - 環境配置
- OpenCV Python - 讀取影像
- OpenCV Python - 寫入影像
- OpenCV Python - 使用 Matplotlib
- OpenCV Python - 影像屬性
- OpenCV Python - 位運算
- OpenCV Python - 形狀和文字
- OpenCV Python - 滑鼠事件
- OpenCV Python - 新增軌跡條
- OpenCV Python - 縮放和旋轉
- OpenCV Python - 影像閾值化
- OpenCV Python - 影像濾波
- OpenCV Python - 邊緣檢測
- OpenCV Python - 直方圖
- OpenCV Python - 顏色空間
- OpenCV Python - 影像變換
- OpenCV Python - 影像輪廓
- OpenCV Python - 模板匹配
- OpenCV Python - 影像金字塔
- OpenCV Python - 影像疊加
- OpenCV Python - 影像混合
- OpenCV Python - 傅立葉變換
- OpenCV Python - 捕捉影片
- OpenCV Python - 播放影片
- OpenCV Python - 從影片中提取影像
- OpenCV Python - 從影像生成影片
- OpenCV Python - 人臉檢測
- OpenCV Python - 均值漂移/CamShift
- OpenCV Python - 特徵檢測
- OpenCV Python - 特徵匹配
- OpenCV Python - 數字識別
- OpenCV Python 資源
- OpenCV Python - 快速指南
- OpenCV Python - 資源
- OpenCV Python - 討論
OpenCV Python - 從檔案中播放影片
VideoCapture() 函式也可以從影片檔案而不是攝像頭獲取幀。因此,我們只需將攝像頭索引替換為要播放的影片檔名即可在 OpenCV 視窗上播放影片。
video=cv2.VideoCapture(file)
雖然這足以開始渲染影片檔案,但如果影片帶有聲音,則聲音不會一起播放。為此,您需要安裝 ffpyplayer 模組。
FFPyPlayer
FFPyPlayer 是 FFmpeg 庫的 Python 繫結,用於播放和寫入媒體檔案。要安裝,請使用 pip 安裝工具,使用以下命令:
pip3 install ffpyplayer
該模組中 MediaPlayer 物件的 get_frame() 方法返回音訊幀,該音訊幀將與從影片檔案中讀取的每一幀一起播放。
以下是播放影片檔案及其音訊的完整程式碼:
import cv2
from ffpyplayer.player import MediaPlayer
file="video.mp4"
video=cv2.VideoCapture(file)
player = MediaPlayer(file)
while True:
ret, frame=video.read()
audio_frame, val = player.get_frame()
if not ret:
print("End of video")
break
if cv2.waitKey(1) == ord("q"):
break
cv2.imshow("Video", frame)
if val != 'eof' and audio_frame is not None:
#audio
img, t = audio_frame
video.release()
cv2.destroyAllWindows()
廣告