'Python add audio to video opencv

I use python cv2 module to join jpg frames into video, but I can't add audio to it. Is it possible to add audio to video in python without ffmpeg? P.S. Sorry for my poor English



Solution 1:[1]

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.

Solution 2:[2]

This is how I am reading audio and video frames:

from moviepy.editor import *
from pafy import pafy

if __name__ == '__main__':
    video = pafy.new('https://www.youtube.com/watch?v=K_IR90FthXQ')
    stream = video.getbest(preftype='mp4')

    video = VideoFileClip(stream.url)
    audio = video.audio
    for t, video_frame in video.iter_frames(with_times=True):
        audio_frame = audio.get_frame(t)
        print(audio_frame)
        print(video_frame)

This code downloads YouTube video and returns raw frames as numpy arrays. You can pass the file as an argument to the VideoFileClip instead of the URL.

Solution 3:[3]

You can use pygame for audio. You need to initialize pygame.mixer module And in the loop, add pygame.mixer.music.play() But for that, you will need to choose audio file as well.

However, I have found better idea! You can use webbrowser module for playing videos (and because it would play on browser, you can hear sounds!)

import webbrowser webbrowser.open("video.mp4")

Solution 4:[4]

import pygame

pygame.mixer.init()
pygame.mixer.music.load(
    'c:Your_file')
pygame.mixer.music.play()

while True:
    pygame.time.Clock().tick()
    #used to show that you can do other stuff while playing audio
    print("hi")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jan Beneš
Solution 3
Solution 4 Nick stands with Ukraine