Skip to content Skip to sidebar Skip to footer

Python - Playing A Video With Audio (with Opencv?)

This is probably a stupid question, and I have searched for this but didn't find a straightforward answer: Can you play a video with audio using OpenCV and FFMPEG? If not, what is

Solution 1:

Use ffpyplayer to handle the audio part.

import cv2
import numpy as np
#ffpyplayer for playing audiofrom ffpyplayer.player import MediaPlayer
video_path="../L1/images/Godwin.mp4"defPlayVideo(video_path):
    video=cv2.VideoCapture(video_path)
    player = MediaPlayer(video_path)
    whileTrue:
        grabbed, frame=video.read()
        audio_frame, val = player.get_frame()
        ifnot grabbed:
            print("End of video")
            breakif cv2.waitKey(28) & 0xFF == ord("q"):
            break
        cv2.imshow("Video", frame)
        if val != 'eof'and audio_frame isnotNone:
            #audio
            img, t = audio_frame
    video.release()
    cv2.destroyAllWindows()
PlayVideo(video_path)

The sample code will work but you need to play around the cv2.waitKey(28) depending on the speed of your video.

Post a Comment for "Python - Playing A Video With Audio (with Opencv?)"