Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 740
olokelo Avatar asked Oct 21 '17 15:10

olokelo


3 Answers

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.

like image 79
DK250 Avatar answered Nov 18 '22 02:11

DK250


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.

like image 31
Jan Beneš Avatar answered Nov 18 '22 04:11

Jan Beneš


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")

like image 21
user13606661 Avatar answered Nov 18 '22 03:11

user13606661