hoothoot
hoothoot

Reputation: 71

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 the best way to do this? It seems so simple but I'm so new to Python that I don't know what to expect/what to search for.

Thank you!

Upvotes: 7

Views: 23206

Answers (3)

Prasad Bebale
Prasad Bebale

Reputation: 11

import numpy as np
import cv2
import pygame  #pip install pygame
from pygame import mixer
mixer.init()

file_name = "Intro1.mp4"
window_name = "window"
interframe_wait_ms = 30

cap = cv2.VideoCapture(file_name)
if not cap.isOpened():
    print("Error: Could not open video.")
    exit()
    
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
mixer.music.load("Intro1.mp3")
mixer.music.play()

while (True):
    ret, frame = cap.read()
    if not ret:
        print("Reached end of video, exiting.")
        break

    cv2.imshow(window_name, frame)
    if cv2.waitKey(interframe_wait_ms) & 0x7F == ord('q'):
        print("Exit requested.")
        break

cap.release()
cv2.destroyAllWindows()

The abow code uses the OpenCV library and the Pygame library to display a video file with a corresponding audio file. Here is a brief overview of what the code does: The code imports the necessary libraries: NumPy, OpenCV, and Pygame. It sets the name of the video file to be played and the name of the window where the video will be displayed.

  1. It creates a VideoCapture object to read the video file.
  2. It checks if the video file was successfully opened, and exits the program if it was not.
  3. It sets the named window to fullscreen mode.
  4. It loads the audio file into the Pygame mixer and starts playing it.
  5. It enters a while loop to read each frame of the video and display it in the named window.
  6. When the end of the video file is reached, the program prints a message and exits. Overall, this code allows you to play a video file with corresponding audio using OpenCV and Pygame libraries.

Upvotes: 0

Vishaka Basnayake
Vishaka Basnayake

Reputation: 21

If the audio does not work the following code can be used

#date: 22 May 2022

#reference:https://github.com/thedreamcode/python_basics/blob/master/read_video_audio.py

import cv2
import numpy as np
from ffpyplayer.player import MediaPlayer


def getVideoSource(source, width, height):
    cap = cv2.VideoCapture(source)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    return cap


def running_videos(sourcePath):
   
    camera = getVideoSource(sourcePath, 720, 480)
    player = MediaPlayer(sourcePath)

    while True:
        ret, frame = camera.read()
        audio_frame, val = player.get_frame()

        if (ret == 0):
            print("End of video")
            break

        frame = cv2.resize(frame, (720, 480))
        cv2.imshow('Camera', frame)

        if cv2.waitKey(20) & 0xFF == ord('q'):
            break

        #if val != 'eof' and audio_frame is not None:
            #frame, t = audio_frame
            #print("Frame:" + str(frame) + " T: " + str(t))

    camera.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    running_videos( r"copy_path_to_video_file_here")

Upvotes: 2

Tamil Selvan S
Tamil Selvan S

Reputation: 678

Use ffpyplayer to handle the audio part.

import cv2
import numpy as np
#ffpyplayer for playing audio
from ffpyplayer.player import MediaPlayer
video_path="../L1/images/Godwin.mp4"
def PlayVideo(video_path):
    video=cv2.VideoCapture(video_path)
    player = MediaPlayer(video_path)
    while True:
        grabbed, frame=video.read()
        audio_frame, val = player.get_frame()
        if not grabbed:
            print("End of video")
            break
        if cv2.waitKey(28) & 0xFF == 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()
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.

Upvotes: 9

Related Questions