I am using this code to capture everything on my monitor and save it to mp4 file:
# screen recording config
sct = mss()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(recording_file_name_location, fourcc, 20.0, (1920, 1080))
# while loop that captures the while screen image by image
while True:
sct_img = sct.grab(screen_resolution)
out.write(np.array(sct_img))
The problem is I want this code to stop recording once I logoff from my Windows machine, that does happen, and the video is saved on my Desktop, but it is not playable.
If I run this code via PyCharm and I end the code via PyCharm (CTRL + C or I use PyCharm stop button), then it does save the mp4 file and I can play it without any problems.
Also, if I start the code from the CMD it works fine, but if I close CMD windows on the "X" in the top right corner, video is again unplayable.
I guess that this happens due to killing Python process in the background, so the script/code never saves the file properly.
Do you guys have any suggestion for this problem? How to save the mp4 video once I logoff or turn off my Windows machine?
Thank you in advance
The process was killed abruptly.
The video is not playable because out.release() could not be called. Tthe video writer was not given the chance to write index structures into the video file, which can only be done when the file is complete.
Simple fix: make a .ts file instead of any other format/extension. These "transport stream" formats never need finalizing.
Perhaps you need to learn about how Windows ends processes when you log off. Usually Windows sends each application/window a WM_QUERYENDSESSION and WM_ENDSESSION message, telling it to end itself before it gets ended by force.
Related
My goal is to set up a webcam with Raspberry Pi to be active during a certain time (let's say 2 minutes). During this time I should be able to capture a still image at any time and save that image to a folder. I chose to work with pygame. My project is about capturing an image as soon as a sensor has been triggered, so it needs to be very responsive.
According to the documentation for Pygame camera here it says:
open()
Opens the camera device, attempts to initialize it, and begins
recording images to a buffer. The camera must be started before any of
the below functions can be used.
get_image()
Pulls an image off of the buffer as an RGB Surface. It can optionally
reuse an existing Surface to save time. The bit-depth of the surface
is either 24 bits or the same as the optionally supplied Surface.
So for my case, the get_image() simply seems to pull out the first image captured after start() has been called. My question is, how can I reach the buffer with all captured images or how does the capturing actually work? I can't find a solution for capturing and saving a still image (at any time) after in between I call start() and stop() on the pygame camera. Since the start() function initiates during a few seconds, it is simply too slow to just call start(), get_image() and stop() after one another. Any help or suggestions would be appreciated.
See my python code below:
def activateCapturing:
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(320,180))
cam.start()
pngdata = None
imageAsbytes = []
activated = True
while activated:
if readSensor():
img = cam.get_image()
pygame.image.save(img, "/tmp/image.png")
activated = False
with open("/tmp/image.png", 'rb') as f:
pngdata = f.read()
imageAsbytes = bytearray(pngdata)
cam.stop()
return imageAsbytes
Thanks in advance!
You simpy do not stop the camera after capturing one image.
See https://www.pygame.org/docs/tut/CameraIntro.html.
get_image() gets the image the camera currently sees from the buffer - the buffer NOT being "all pictures since start()" but simply the currently viewed image.
You use stop() after your 3s of "capturing window" to stop aquiering more images.
If you are after performance, you might want to scroll down that page and review the section about Capturing a Live Stream - if you did the same (w/o displaying the stream) and just saved 1 image to disk when needed you should get a decent framerate.
Api: get_image()
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.
I am trying to change the video of omx player in python based on an event. But the problem is the next video gets loaded only after the first video finishes.
import os
os.system('omxplayer video1.mp4')
if(flag==1):
os.system('omxplayer video2.mp4')
Is there a way to interrupt video1 in middle of playback and start second video.
Note:I am working on a raspberrypi with debian wheezy and python 2.7
Omxplayer has keyboard controls so you could send the corresponding keystroke q to stop it normally before opening the next instance. Here is a similar post where a person tried to achieve similar feature, it could be helpful.
I'm trying to learn SimpleCV using Python 2.7 in IDLE.
Once the camera form SimpleCV is initialized the camera become unavailable to other programs like native webcam application or skype etc.
from SimpleCV import *
camera = Camera()
After restarting the pc or logoff and logon the webcam becomes to those applications. It seems that even closing out from python IDLE, it doesn't close the camera stream. Is there any way to stop the camera stream of simplecv?
I couldn't replicate your issue, but if the webcam is still running even after your program terminates/you close IDLE, you can end the camera by going into task manager and killing all running Python processes.
After some experimenting, I found that if you want to accomplish the same thing directly inside the code, you could try simply deleting the reference altogether:
>>> import SimpleCV as scv
>>> cam = scv.Camera()
>>> del cam
Calling del cam caused the webcam indicator light on my laptop to turn off. Granted, this appears to be an undocumented (??) solution, so I'm not sure how robust it is. I would probably try testing this on several different laptops/webcams first, to make sure it works reliably and consistently.
I am making an online webcam for my parents, using raspberry pi. I want it to capture a photo, upload it to a webserver, then upload a copy to a different server for archiving. I use the script streamer to snap a still from the webcam. It works, the problem is that it seems that streamer sometimes crashes, looping the error message "v4l2: oops: select timeout". This can happen after a few shots, or after 10 minutes of operation, it seems random. I have added a command that kills the streamer process after each snapshot, it did make the program a bit more stable, but eventually it still gets stuck in the error loop. I don't know what the problem is or even how to debug it.. What can I do?
I am using raspbian with the included drivers. The webcam is logitech c200. I first tried using opencv to capture stills, but couldn't get it to work properly. If someone could help with that maybe it would fix the problem, I don't know..
This is the code, it's python:
import time
import sys
from subprocess import call
import ftputil
while True:
call("streamer -q -f jpeg -s 640x480 -o ./current.jpeg", shell=True)
time.sleep(0.2);
call("killall -q streamer", shell=True)
filename = str(time.time()) + ".jpg"
host = ftputil.FTPHost(*****)
#host.remove("/domains/***/public_html/webcam.jpg")
host.upload("./current.jpeg", "/domains/***/public_html/webcam.jpg", mode='b')
host.close()
host = ftputil.FTPHost(****)
#host.remove("/domains/***/public_html/webcam.jpg")
host.upload("./current.jpeg", "/webcamarchive/"+filename, mode='b')
host.close()
time.sleep(10);
Never mind, used pygame instead:
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
image = cam.get_image()