Moviepy write_videofile works the second time but not the first? - python

I'm concatenating a list of video objects together then writing them with write_videofile, weirdly enough the first time I write the file, it plays fine for the first halfish then the first few frames of each clip in the file afterwards plays before freezing. But here's the odd part, If I write the exact same video object right after the first video writes, it writes just fine and plays perfectly.
Here's my code
from moviepy.editor import VideoFileClip, concatenate_videoclips
clipslist = []
clips = ['https://clips-media-assets2.twitch.tv/AT-cm%7C787619651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787628097.mp4', 'https://clips-media-assets2.twitch.tv/2222789345-offset-20860.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787624765.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787539697.mp4', 'https://clips-media-assets2.twitch.tv/39235981488-offset-3348.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788412970.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787682495.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787962593.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787627256.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787573008.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788543065.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787593688.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788079881.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788707738.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788021727.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787595029.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9536.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788517651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788087743.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787497542.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9154.mp4', 'https://clips-media-assets2.twitch.tv/7109626012888880881-offset-4818.mp4', 'https://clips-media-assets2.twitch.tv/72389234-offset-760.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787774924.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787565708.mp4']
for clip in clips:
dlclip = VideoFileClip(clip, target_resolution=(1080, 1920)) # Download clip
clipslist.append(dlclip)
videofile = concatenate_videoclips(clipslist)
videofile.write_videofile("final1.mp4") # Broken after the first halfish
videofile.write_videofile("final2.mp4") # Works entirely fine.
videofile.close
Any ideas? Any suggestions appreciated.
Sometimes when the video is small enough it seems to write the first time just fine too.
It seems there is no set point where it breaks, each time I write it for the first time it typically breaks at a different spot.
I've tried waiting for the thread to exit and sleeping after the concatenation and that doesn't seem to fix the issue.

If you cannot consistently replicate the issue, it's most likely not an issue with your code.
Try opening the produced clip with a different program, such as VLC.

I came across with the same problem when writting multiple videos at the same time with write_videofile, it seems like the later tasks will cause the wrong outputs of the previous write_videofile tasks by hanging their writting processes, although the processes will continue after the later tasks finish, the result videos of previous tasks break at the hanging spots, haven't found a solution

Related

ROS Python main loop hanging for few seconds

I have a problem in ROS 1 Noetic and Python in Ubuntu 20 on Raspberry Pi. I am running a few other nodes and the main node. In the main node it often happens that the code randomly hangs. This happens in a callback which receives information from another node. But it also happens if I only run the main loop. For simplicity purpose, I am attaching image of how that happens in the main loop. The first image shows the code and the second image shows the output.
As seen from the second picture, the main loop randomly hanged for whole 2.5 seconds.
If you need some more information, I would be happy to provide it.
Finally I have found the problem. It wasn't with the code, but with the SD card. When I switched to another SD card, the problem was gone.
This is the SD card which had the problem:
https://www.amazon.com/Kingston-Digital-32GB-microSDHC-SDCA3/dp/B00RVNGGT8?th=1
And this is the one which didn't:
https://www.amazon.com/Sandisk-Ultra-Micro-UHS-I-Adapter/dp/B073K14CVB?th=1
The one which didn't have the problem has significantly greater read speed. I guess this is the difference that caused the problem. Or maybe the card I used before was faulty in some other way.
What do you think? Could the read speed have such an impact?

Raspberry: Showing various images on display according to GPIO states

I would like to have some advice, what would be the best solution to my problem. I want to write a python program, that runs on Raspberry Pi. It runs in an infinite loop, and checks various stuff, like contents of some folders, and GPIO states.
A basic operation looks like this:
pictures arrive in a folder from a different source
the code notices, reads the images, does some processing
it starts to listen to GPIO input: in case of GPIO state change, the code displays the image.
if the GPIO state changes again, the code switches the picture to an another. And the loop resets, files are deleted, and the cycle starts again
I solved most of the problems, but I have difficulties with displaying the images.
I tried using opencv, I structured the code something like this, it's only a dummy:
state = "first_phase"
while True:
state = decide_state() #this part checks everything,
GPIO, folders, etc
if state = "first_phase":
cv2.imread(some_picture)
cv2.imshow(the picture)
cv2.waitKey(1500) #I had freezes with less delay, but this seems to work
state = "next_phase"
continue
elif state = "next_phase":
cv2.imread(some_other_picture)
cv2.imshow(the_picture)
cv2.waitKey(1500)
state = "first_phase"
continue
destroyAllWindows()
This is much much more complex in reality, with way more states and GPIO ports used, it really is a difficult application, but the idea is something like this, and it is working. 99% of the time...
1% of the time I get weird errors, like "GDK_IS_DEVICE" and Segmentation Faults. (I checked, resources are ok) I can't resolve them, and they come erratically, sometimes right away, sometimes after the 994th picture, and always at the point where the code tries to show the images.
I'm now convinced that it has to do with the highgui opencv uses, but - you can probably guess from my code - I have not enough experience to debug this. I cannot completely omit using opencv because I'll also do some operations on the images, but for displaying them I think I should use an another method. PILLOW is not okay, because I don't want to write out the results of the image manipulations, only draw them on the display. (also it is only possible to kill the display with subprocesses, and I don't feel comfortable doing that). I tried using tkinter but I'm lost how threading and communicating between my main loop and the tkinter loop would be possible and crash-free.
I can restart the code, but the last images will be lost, and it is important to show them again, if the code crashed. I could write a workaround, where I write the manipulated pictures on the disk, and after a successfull display it deletes them, so after starting up again, it sees that there are still images left to show, and shows them... but this only solves the symptoms, and also kills the sd card faster.
Is there any other solution to this? TL;DR: I want to draw pictures on the display in case of some events, while my code does it's thing in the background for eternity.

cv2 error while in execution Bad file descriptor

I'm running some Python scripts in a loop (but not so fast).
example:
import time
import cv2
stream = cv2.VideoCapture(0)
while True:
ret,frame = stream.read()
cv2.imshow("Frame",frame)
cv2.waitKey(1)
time.sleep(10)
After some minutes of execution I'm getting this error:
E1205 11:19:12.803174714 32302 backup_poller.cc:132] Run client channel backup poller: {"created":"#1607177952.802346313","description":"pollset_work","file":"src/core/lib/iomgr/ev_epollex_linux.cc","file_line":324,"referenced_errors":[{"created":"#1607177952.802226759","description":"Bad file descriptor","errno":9,"file":"src/core/lib/iomgr/ev_epollex_linux.cc","file_line":954,"os_error":"Bad file descriptor","syscall":"epoll_wait"}]}
Does someone know what is this? I looked into Google but I didn't find any good solutions.
after creating a VideoCapture, always test for stream.isOpened(). if that is False, you can't do anything to the video.
after ret, frame = stream.read(), always test if ret is True. if it's False, the video has ended and you need to end the loop.
I would also recommend not simply sleeping for ten seconds. during that time, the GUI event loop won't run (because you aren't running waitKey), which makes the imshow window unresponsive and your OS might decide to kill the process.
use waitKey(10000) instead. check if waitKey() waited the full interval. it may return earlier because of a key event. you could accept that or repeat waitKey with the remaining time.
a camera will produce frames in regular intervals, no matter if you read them or not. if you don't read them, they queue up. the driver will not like that and may decide to throw errors at you.

How to run tasks periodically without interrupting the whole program

I have a program that constantly runs if it receives an input, it'll do a task then go right back to awaiting input. I'm attempting to add a feature that will ping a gaming server every 5 minutes, and if the results every change, it will notify me. Problem is, if I attempt to implement this, the program halts at this function and won't go on to the part where I can then input. I believe I need multithreading/multiprocessing, but I have no experience with that, and after almost 2 hours of researching and wrestling with it, I haven't been able to figure it out.
I have tried to use the recursive program I found here but haven't been able to adapt it properly, but I feel this is where I was closest. I believe I can run this as two separate scripts, but then I have to pipe the data around and it would become messier. It would be best for the rest of the program to keep everything on one script.
'''python
def regular_ping(IP):
last_status = None
while True:
present_status = ping_status(IP) #ping_status(IP) being another
#program that will return info I
#need
if present_status != last_status:
notify_output(present_status) #notify_output(msg) being a
#program that will notify me of
# a change
last_status = present_status
time.sleep(300)
'''
I would like this bit of code to run on its own, notifying me of a change (if there is one) every 5 minutes, while the rest of my program also runs and accepts inputs. Instead, the program stops at this function and won't run past it. Any help would be much appreciated, thanks!
You can use a thread or a process for this. But since this is not a CPU bound operation, overhead of dedicating a process is not worth it. So a thread would be enough. You can implement it as follows:
import threading
thread = threading.Thread(target=regular_ping, args=(ip,))
thread.start()
# Rest of the program
thread.join()

PyAudio is clipping the end of sound

I have written/am still playing with a software FSK modem written in python using PyAudio. At first I was modulating the sound into a *.wav file and then playing it at a later time however this is not a long term solution. I have put in place code which takes some given input, prepares the 8N1 data to be sent and then attempts to play it.
My problem is that the sound itself does not seem to play fully; I have verified this by noting that the demodulator produces the correct message sans the last few characters. Recording the wave output by the modulator also corroborates this observation.
I think that the problem has something to do with the frames_per_buffer however I don't know what is going on. I have tried to appeal to PyAudio itself by just sending all the data at once (in a stream.write(...)) however this fails. I have also tried sending it a chunk at a time (as in the case of http://people.csail.mit.edu/hubert/pyaudio/). I am not, however, playing from a file, rather from an array of samples.
What could the problem be?

Categories