Skip to content

Instantly share code, notes, and snippets.

@brunomsantiago
Created April 8, 2021 12:30
Show Gist options
  • Save brunomsantiago/db63b103b9fb8bebda6aed234f3f13a6 to your computer and use it in GitHub Desktop.
Save brunomsantiago/db63b103b9fb8bebda6aed234f3f13a6 to your computer and use it in GitHub Desktop.
Batch rename (serialize) video files
from pathlib import Path
# Input parameters
folders = ['/my/video/folder1', '/my/video/folder2']
video_file_types = 'avi flv m4v mkv mov mp4 mpg wmv'.split(' ')
# Folders as pathlib objects
folders = [Path(folder) for folder in folders if Path(folder).is_dir()]
# List and sort all files from all folders
all_files = []
for folder in folders:
files = folder.rglob('*')
files = [f for f in files if f.is_file()]
all_files.extend(files)
del files
all_files = sorted(all_files, key=lambda x: x.name)
# Keep only video fidles
video_files = [f for f in all_files
if f.name.split('.')[-1] in video_file_types]
# Rename all video files
for n, file in enumerate(video_files):
name = file.name
# New name remove the first 4 chars and then add a new serialization prefix
new_name = f'{n+1:04d} {name[5:]}'
print(name)
print(new_name)
print('')
new_filepath = file.parent / new_name
file.rename(new_filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment