Python play audio in sync with cv2 video - python

I am very new to the cv2 library, and since I recently discovered it is unable to play audio I wanted to know if there is another library that will allow me to play the audio in sync with the video itself (preferably without needing an audio file, just using the video's audio)

Turns out there is a very simple method that I will found after researching later, unfortunately, it requires a separate audio file that matches the video:
First, import simpleaudio as sa (install it with pip if necessary) and then add this piece of code to your cv2 video player. Add it before the while loop but after the line of code that defines the video file:
wave_obj = sa.WaveObject.from_wave_file("AudioFile.wav")
wave_obj.play()
Then you must manually adjust the waitKey until the audio matches the video, otherwise it might be too fast or too slow. Generally a value close to 25 must be used, and if the audio abruptly ends, try adding one to the value until it matches

Related

Extract audio from a MPEG(.ts) file in memory in python, Without writing MPEG to a file

I am working on a project that need to extarct audio from a stream which is transmitted by .ts(MPEG-2 Transport Stream) file.
Currently I need to First save the file to file system, Then open it using moivepy to convert to WAV format audio.
The streaming requires realtime transmit, and there are multiple .ts file need to be process every second, Moivepy is too slow to open them all and convert each in realtime.
So I wonder if I can finish the whole process of extracting audio from MPEG in memory, avioding file system IO may speed up the process. How can I do it?
You can possibly try the ffmpeg-python package where you can take a look at the -target flag in the output function and specify .wav file output. https://ffmpeg.org/ffmpeg.html#Synopsis. Most flags in the synopsis page are offered in the package. I haven't yet encountered one that is not offered.
python-ffmpeg python bindings documentation
Example code:
import ffmpeg
audio_input = ffmpeg.input(url)
audio_output = ffmpeg.output(audio_input, save_location, target='filename.wav')
audio_output.run()

How to stream a video with youtube-dl in Python 3

I would like to stream the audio of a youtube video in python, youtube-dl allows me to download a video (audio in my case), but that process can take some time. My objective is to be able to stream the audio 'dynamically', like I would by going on a youtube video. I would like to start playing the audio and still download the rest of it at the same time.
I know that the youtube-dl command line program allows to stream videos to media players, like VLC for example:
youtube-dl -o - -- "[videoID]" | vlc -. I could create a subprocess and execute that command, but I would prefer to use a cleaner way, if possible.
I would expect to have some sort of data that I can transmit to an audio device later on. I don't need to store the audio in a file, but it's not a big deal if there is a temporary file.
This is unfortunately not possible. Youtube-DL does not expose an API that makes this straightforward. This is where Youtube-DL opens the file (or stdout) for writing. It's not exactly written to allow for easy switching of the output stream.
It's probably easier to just subprocess it and pipe its output if you really want this functionality.

How can I record audio in Python?

I'm wondering if there are libraries in Python for audio recording.
Ideally, I need to get a cross-platform solution, how can I record audio through Python.
Ideally, in order that in this library I can either pause with the ability to listen to the recorded fragment and remove it from the audio recording, or record several audio files and after the program work, glue it into 1 audio file.
If someone knows such a decision, I will be happy to receive help.
Thanks in advance!
I would look into python-sounddevice.
https://python-sounddevice.readthedocs.io/en/0.3.11/
It can record and play as well as convert your music into a numpy array if that every interested you. It is a python handler of the PortAudio which is cross-platform.
http://portaudio.com

Having python script save captured video with date and time

I'm trying to have my raspberry pi capture short videos and save them to a file with date and time. I have a short Python script that will capture video but it seems to overwrite the file with the latest video instead of saving each capture to a separate file.
import time
import picamera
with picamera.PiCamera() as camera:
camera.start_preview()
camera.start_recording('/home/pi/Desktop/video.h264')
time.sleep(60)
camera.stop_recording()
camera.stop_preview()
Of course it will.
Since you're always using the same path and file name to save the file it will override whatever was in the file video.h264 before. You should implement an algorithm to avoid this.
You could name the files with the recording date in it.This will usually never be the same and you can rename your files later if you want so. The system date is available in the standard time module.
When you hit into problems with that we can help you.

Why won't a .mpg video load into pygame's movie module

I've been trying to get a short video to load and play within a pygame window. However, the program stalls when it tries to load the video. I'm also unable to abort the debugger.
import pygame
import pygame.movie
pygame.init()
print('TEST')
video = pygame.movie.Movie('D:\\Presentation\\video.mpg')
print('TEST')
This outputs 'TEST' once, so the video has not loaded (It keeps running "pygame.movie.Movie('D:\Presentation\video.mpg')" indefinitely).
I let it run for an hour but no luck, it keeps loading without any kind of exception or visible progress and the video is 1,636 KB large, 4 seconds long and was converted from a .mp4 using ffmpeg.
The directory is certainly correct for I tested with images in the same directive. I also restarted my PC (Windows 8.1 64-bit Python 3.2.5.1).
EDIT: I need an answer that will be relevant to the pygame module
I suggest you use Pyglet and follow the code I create here that plays a video.

Categories