Having python script save captured video with date and time - python

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.

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

Python play audio in sync with cv2 video

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

How to record rtsp stream to file in python

I need to record a rtsp video stream to file in python, I want to stop the recording at anytime depending on the situation. I used subprocess to interact with ffmpeg command line, but it breaks sometimes, the video keep recording, resulting in a huge file.
Is there any good way to dump a rtsp video stream to file, with the capability that you can stop the recording anytime after you start recording?
Any help is much appreciated.
ADDED:
Preferably the similar API as the PiCamera like so:
import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_recording('video.h264')
sleep(5)
camera.stop_recording()
But the only difference is that the source of video is the IP camera.

Python code to play an audio file

I want to build a school alarm application using python and raspberry pi.
How can i use use python to play an audio file at specified timings?
This will work if the alarm noise file is in the same directory as the python file itself. I do warn you though this is very clunky and the sound file is opened separately but nevertheless it does what you required.
import os, time
while True:
os.startfile("FILENAME OF SOUND") #Replace with the appropriate file name (Make sure the alarm sound is in the same directory as the python file else this will not work)
time.sleep(DELAY IN SECONDS) #This is how long the delay is between each time it opens the sound

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