Function to increment the file name - python(opencv)

Asked

Viewed 54 times

0

i have this code that saves me a video every time I run the program with the name video0.

out = cv2.VideoWriter(video0.avi', fourcc, fps, (int(width), int(height)))

I wanted him now every time I ran the show to keep the video under another name. First time :video0 2nd time :video1 3rd time :Video2

1 answer

0


All you have to do is this:

import os

filename = 'video0.avi'
if os.path.exists(filename):
  tmp = filename.split('.avi')
  count = tmp[0].replace('video', '') or 0
  count = int(count) + 1
  new_filename = 'video{0}.avi'.format(count)

Basically, you check if it exists on disk, you will need the path to the file location, then split by the point and the file’s notation then remove the base name and increment the result, if it does not exist with "or 0" adds a defalt.

The same with function.

import os

def load_file(filename):
  if os.path.exists(filename):
    tmp = filename.split('.avi')
    count = tmp[0].replace('video', '') or 0
    count = int(count) + 1
    return 'video{0}.avi'.format(count)
  return filename

filename = 'video.avi'
print(load_file(filename))

All over the board.

import os, re, os.path

def load_file(file):
  number = []
  onlyfiles = [f for f in os.listdir('.') if os.path.isfile(os.path.join('.', f))]  # load all dir files
  filename, file_extension = os.path.splitext(file)                                 # load filename and extension from parameter file
  total_files = len(onlyfiles)                                                      # get total files in dir
  splitter = re.compile(r'\d+')                                                     # build a splitter to get digits part of file name
  number = splitter.findall(filename) or ['0']                                      # get number part from filename
  only_name = filename.replace(number[0], '')                                       # get only name from filename removing the number from it
  i = 0
  matching = [s for s in onlyfiles if only_name in s]                               # build array only with files names that match the only_name (from file name)
  if len(matching) == 0:                                                            # if no files matchs the criteria returns the name itself, don't exist any
      return file
  while i < len(matching):                                                          # interates only the array of matching files and extracts the number from it
    number.append((splitter.findall(matching[i]) or [0])[0])                        # add to number array the numbers from file found
    i += 1
  number = [int(i) for i in number]                                                 # converts all number array to int (cast), some of then are strings
  largest_number = max(number)                                                      # get the max value from array
  return '{0}{1}{2}'.format(only_name, str(largest_number + 1), file_extension)     # returns the new file number

filename = 'video.avi'
print(load_file(filename))

out = cv2.VideoWriter(load_file(filename), fourcc, fps, (int(width), int(height)))
  • and this in function form?? any idea? where the argument was the name of the output.

  • That’s very easy, take a look now.

  • oh yes sure xd, thank you very much!

  • Okay, it works but it always picks up the filename video. After it runs again it picks up the video 0 again instead of video1b (which should generate Video2). basically wanted to take the ULTIMO and increase. some idea?? sorry there and thanks :c

  • You need to go through the entire directory and check if there is a file with that name, this function is checking if the one you pass to it exists.

  • and how I would walk the board?

  • Take a look, I already put this function with search, confirms if it works. I have no way to test.

  • Hmm thank you, I’ll test it now

  • it will always fetch filename = 'video1.avi', creates Video2.avi, but then never creates video3.avi etcc, replaces Video2.avi

  • I figured out why, I was validating with if out of while, it has to be while first, go through the entire directory looking for the file, if it finds goes to the maximum found for if and returns that maximum + 1. Now test.

  • remains the same, nor do I understand why, the code seems.me to be correct, it replaces in the same the last generated video (Video2.avi)

  • I mean, now not even trouble for Video2, keeps modifying video1, I was wrong on the last comment

  • Lol...that basic error...I forgot the i += 1, it was always at zero.... he eh, try now

  • just changing the video 1 :( I’m already without faith ahah :c, no longer understand anything xd

  • Debug, try to figure out what’s missing there.

  • yes that’s what I’m doing, thanks for the help!

  • Hi, I decided. The solution was a little far away. Take a look now. I had to create a local project to test, yesterday I couldn’t. I left notes in the code so you can figure out what step by step is. It might not solve everything, but at least existing a video.avi file and other video10.avi, it will find the biggest and will return video11.avi..

  • Thank you in advance! What you’re doing is taking the video with the highest number (e.g., video1), creating a Video2 (which n works) and creating a video3 (which is, what it’s supposed to be).. consecutively picks up the video 3, creates a video 4 that does not work and also creates a Video5 that is what is supposed to.. but this with a bit of debug should go to the place

  • Now that I’ve got it tested, I don’t think you’re doing anything right. Look at the line I added to the code, that’s how you’re using it, with the base name, 'video.avi'...if you pass like this, it seems to me that it’s okay.

  • the problem is that I call the function twice, but I already realized the error, I will already change my code. thanks for the help

Show 15 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.