1

So I have a VideoCapture that gets the image of an camera, and when some event happens it starts to record.For a fact i know that my image is 640x480 and when I use cv2.imwrite it saves image all great.I set my VideoWriter to record 1 Frame per second and It should last 10 seconds as I have a for loop that goes 10 times.And file has around 1MB of data but frames are all black nothing else.And video length is ok(10 seconds as expected).

        Writer = cv2.VideoWriter(FileName, cv2.VideoWriter_fourcc('M','J','P','G'), 1, (640, 480))
        SeccondsOfFootage = 10
        for i in range(0, SeccondsOfFootage):
            print(i)
            Writer.write(Cam.frame)

        Writer.release()
Vuk Uskokovic
  • 155
  • 10
  • Can you share your code? – Argon Aug 25 '19 at 13:54
  • @Argon see the edit. – Vuk Uskokovic Aug 25 '19 at 14:00
  • `for i in range(0, SeccondsOfFootage):` won't actually hold your program for 10 real seconds. See [this question](https://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-raw-input) to understand, how to hold the program for some amount of time before allowing it to exit. – Argon Aug 25 '19 at 14:29
  • @Argon I know but on windows that works with no problem but on my raspbian it does not. – Vuk Uskokovic Aug 25 '19 at 14:37
  • A minor maneuver can be done via the `time` library. You can store the starting timestamp in a variable, say `start`. Put your `VideoWriter` object in a loop where you can check if the time has exceeded 10s by subtracting the `start` value from the recent timestamp value (let this be the loop condition too). As long as this time difference is less than 10 you can write your frames otherwise break the loop. Note that this is a crude solution, hence the looping can just overstep the 10s bound by a fraction. – Argon Aug 25 '19 at 15:10
  • @Argon I did use time.sleep(1) on every for loop ticks and it still did the same – Vuk Uskokovic Aug 25 '19 at 15:54
  • This is not enough code. Where is the frame captured? Can you post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – J.D. Aug 25 '19 at 18:57
  • 1
    Don't use `time.sleep()`. It's meant for pausing your program execution for the given amount of time. What your case needs is basically a method via which you can monitor time as well as capture your frames concurrently. That's why I mentioned earlier, to capture a timestamp(say `t0`) right before the loop (via `time.time()`). And while the loop goes on, you keep on capturing the timestamps(say `t1`) at every iteration. If that time difference is less than 10s (`t1-t0<10`) then capture the frame in that iteration else break out of the loop. – Argon Aug 26 '19 at 09:58

0 Answers0