cv2播Video+声音 cv2+ffpyPlayer 【附码】

cv2 只有影像,没有声音。 只播影像简单方法如下:
上一篇 cv2影像播放方式
如果还要加上声音,就要借助 ffpyPlayer了,基本写法如下:

from ffpyplayer.player import MediaPlayer

filename = 'YourVideo.mp4'
player = MediaPlayer(filename)
val = ''
while val != 'eof':
     frame, val = player.get_frame()
     if val != 'eof' and frame is not None:
         img, t = frame

不过,要先安装 ffmpeg,安装设定方式,可参考官方说明
现在,把两者加在一起,让cv2 播影片时有声音了。( 按Esc中断播放 )
Source Code GitHub

''' videoCVffp.py 
    cv2 + ffpyPlayer 播放 Video + sound
    请先安装 ffmpeg     
'''
import cv2
from ffpyplayer.player import MediaPlayer

filename = 'YourVideo.mp4'

# cv 设定
video = cv2.VideoCapture(filename)
FPS = int(video.get(cv2.CAP_PROP_FPS))  # Frames per Sec
cv2.namedWindow('video',cv2.WINDOW_KEEPRATIO) 
cv2.resizeWindow('video', 500,300) 
cv2.moveWindow('video',300,200)

# 声音 设定
player = MediaPlayer(filename)

# 开始播放
val = ''
while val != 'eof':
    
    # 声音在此
    audio_frame, val = player.get_frame()
    
    if val != 'eof' and audio_frame is not None:
        img, t = audio_frame
        print(val, t, img.get_pixel_format(), img.get_buffer_size())
         
    # 影像在此
    ret, frame = video.read()
    
    # if 影片末尾
    if not ret:
        print("End of video")
        break
    
    # if 按Esc 中断
    if cv2.waitKey(FPS) == 27:
        break
    
    cv2.imshow('video', frame)
  
#--- ending
video.release()
cv2.destroyAllWindows()
player.close_player()

<<:  RISC-V on Rust 从零开始(9) - 实作memory model

>>:  容器化及容器技术(containerization and container technology)

DAY 24 『 客制化文字输入框 Custom TextField 』

昨天介绍完客制化按钮,今天会分享客制化文字输入框( 加入图示、图示显示在左边或右边 ) 成品: 刻好...

第 21 集:Bootstrap 客制化 utilities(上)

此篇会教学如何将 Bootstrap 通用类别 utilities 自干一个出来。 事前准备: 原...

Day 23 介绍 FactoryBot Rails 及设定

该文章同步发布於:我的部落格 首先,我们一样先介绍基本的安装以及设定,当一切就绪的时候,写测试这件...

DAY 6 ROS 通讯架构2

前言 今天我们要来讲 ROS 中最为核心的部分,ROS 提供了 四种通讯方式,分别为 Topic、S...

谈谈SQLite

在资料储存的使用上,除了Log、text、dump data,最实用的会是依附在(嵌)每个应用程序的...