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.
Related
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.
I'm having trouble playing music in the background of my program in replit. I have tried using Pygame but couldn't get that to work, so I followed the official replit tutorial video.
I get the confirm audio screen but nothing plays, my file is .wav and is 29 secs long and plays when I go directly onto the file, but not when the program runs. I have uploaded the file into replit so it appears alongside main.py.
Any help would be appreciated, let me know if any additional info is needed
My code:
from replit import audio
source = audio.play_file("music.wav")
while True:
pass
For some odd reason, replit doesn't actually let you run code alongside the playing of music. Get rid of
while True:
pass
It should work then
Look at this, Replit has trouble using
while True:
pass
If you want to do nothing while the audio file is running, instead use time.sleep. You should also be able to run code while the file is running with no problems.
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
In python 3.4 , I was trying to open a "wav" file using vlc in Linux. Here is my code:
import os,time
os.system("cvlc audio/some.wav")
time.sleep(3) #audio was one and half sec
a = 3+3
print (a)
It plays the audio but then doesn’t do the rest. What should I do to make it do them? more precisely what should I do to close the vlc program?
With solving the problem it will also be very grateful to know is there any easier way to play audio within the code specifically in python 3.4?
(platform independent code will be even more grateful!)
So the VLC player doesn't exit. The VLC player has a command line argument to close the player once the song/video has been played.
Playlist
These options define the behavior of the playlist. Some of them can be overridden in the playlist dialog box.
--play-and-exit, --no-play-and-exit
Play and exit (default disabled)
Source: https://wiki.videolan.org/VLC_command-line_help
Can you try the following?
os.system("cvlc audio/some.wav --play-and-exit")
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.