Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 3:11 pm

Hi
I was taking videos from raspicam.
It was working.
But whenever i went to take another video the old video would be overwritten.
I wanted to know how to save videos with different names each time .
For eg. "video1.mp4", "video2.mp4", "video3.mp4"

It would be really helpful if some one shares some Python knowledge .
Thanks.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 10293
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 4:05 pm

Duplicate of viewtopic.php?f=63&t=205972&p=1276052

You're the one calling "camera.start_recording('my_video.h264')" or similar (NB not an mp4 file, just the raw elementary stream), so you need to do something to form a string that fulfils your criteria.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 4:18 pm

Hi,
I removed the duplicate post.
Till now I have achieved to do this.

Code: Select all

#!/usr/bin/python
import time
import os
import sys
import re
import datetime
import subprocess
#from subprocess import call

#your_command = "raspistill -o videostaken/timelapse%04d.jpg -t 100000 -tl 5000 &"


inputimages = "timelapse%04d.jpg"
counter = 1
outputvideo = "myvideo.mp4"

def taketimelapsevideo():

	subprocess.call(["raspistill", "-o", "timelapsetaken/"+inputimages, "-t", "10000", "-tl", "2000"])


	subprocess.call(["avconv", "-r", "10", "-i", "timelapsetaken/"+inputimages, "-r", "10", "-vcodec", "libx264", "-vf", "scale=1280:720", "timelapsetaken/"+outputvideo])


if __name__ == '__main__':
        taketimelapsevideo()

i used avconv instead of mencoder to encode the h264 raw video into mp4 format.
I wanted something like %04d to increment on the next run of the script and not overwrite the old file.
How to do this. Please help . Thanks.

gordon77
Posts: 5383
Joined: Sun Aug 05, 2012 3:12 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 5:19 pm

try this...

Code: Select all

#!/usr/bin/python
import time
import os
import sys
import re
import datetime
import subprocess
#from subprocess import call

#your_command = "raspistill -o videostaken/timelapse%04d.jpg -t 100000 -tl 5000 &"


inputimages = "timelapse%04d.jpg"
counter = 0

#write counter file if first time
if os.path.exists('last.txt') == False:
    file = open("last.txt", "w")
    file.write(str(counter))
    file.close()
    
#read last counter file stored
file = open("last.txt", "r")
counter = int(file.read())
file.close()

def taketimelapsevideo(counter):

   subprocess.call(["raspistill", "-o", "timelapsetaken/"+inputimages, "-t", "10000", "-tl", "2000"])
   counter +=1
   # write counter file
   file = open("last.txt", "w")
   file.write(str(counter))
   file.close()
   outputvideo = "myvideo" + str(counter) + ".mp4"
   subprocess.call(["avconv", "-r", "10", "-i", "timelapsetaken/"+inputimages, "-r", "10", "-vcodec", "libx264", "-vf", "scale=1280:720", "timelapsetaken/"+outputvideo])


if __name__ == '__main__':
   taketimelapsevideo(counter)

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 6:04 pm

Hi friend,

I get an error when i try to run the code:

Code: Select all

TypeError at /taketimelapsevideo
taketimelapsevideo() takes exactly 1 argument (0 given)


I am using python 2.7.9.
Thanks

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 6:19 pm

Hi buddy, I have solved the error.
I had to put counter in taketimelapsevideo() function in my views.py.
And defined it globally.
Last edited by Guneshs on Wed Feb 21, 2018 6:24 pm, edited 1 time in total.

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 6:23 pm

Hi,
It didnt worked .
i get this warning

Code: Select all

File 'timelapsetaken/myvideo1.mp4' already exists. Overwrite ? [y/N] Not overwriting - exiting
Since this is meant to be an automated script , I cant put the "y" to overwrite everytime.

Its not populating the counter on d second run.

gordon77
Posts: 5383
Joined: Sun Aug 05, 2012 3:12 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 7:15 pm

Guneshs wrote:
Wed Feb 21, 2018 6:04 pm
Hi friend,

I get an error when i try to run the code:

Code: Select all

TypeError at /taketimelapsevideo
taketimelapsevideo() takes exactly 1 argument (0 given)


I am using python 2.7.9.
Thanks
I don't understand that. Try deleting last.txt and try this original code again.


gordon77
Posts: 5383
Joined: Sun Aug 05, 2012 3:12 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 7:35 pm

Guneshs wrote:
Wed Feb 21, 2018 7:24 pm
I solved that error
There wasn't that error when l ran it, so it's confusing. It sounds like it's not reading counter and/or not incrementing it.
Try deleting last.txt and changing the line counter =O to counter =1
Last edited by gordon77 on Wed Feb 21, 2018 7:41 pm, edited 1 time in total.

Guneshs
Posts: 48
Joined: Sun Jun 25, 2017 4:18 pm

Re: saving videos taken with raspicam with names in incremental order

Wed Feb 21, 2018 7:36 pm

ohh.... I had added that code to my another script.
SO it gave me error .

gordon77
Posts: 5383
Joined: Sun Aug 05, 2012 3:12 pm

Re: saving videos taken with raspicam with names in incremental order

Thu Feb 22, 2018 8:40 am

Guneshs wrote:
Wed Feb 21, 2018 7:36 pm
ohh.... I had added that code to my another script.
SO it gave me error .
I would assume you are calling taketimelapsevideo without the (counter)

Return to “Advanced users”