I have a lot of videos stored in different subfolders. All the videos have weird file names like f4vd5sd1b5dsd41s415d
. And I have a text file where there are the correct file names of each file. To know which video goes with which title, I added the file duration of the video I want to correspond with the title, so in my text file there is:
in line 1: the name of video 1
in line 2: the video length of video 1
in line 3: the name of video 2
in line 4: the video length of video 2
To better explain it here's some work I did manually:
Before:
After:
My script is:
import os
import shutil
from moviepy.editor import VideoFileClip
# parses the renaming rules described in your text file
# returns a dictionary where:
# key represents the duration to match
# value represents the name to rename the video file to
def parse_rules(path):
with open(path) as file:
lines = file.readlines()
rules = { lines[i+1]: lines[i] for i in range(0, len(lines), 2)}
return rules # structure { duration: rename_to, ... }
# gets the duration of the video file
def duration(file):
return VideoFileClip(file).duration
# determines whether the durations are close enough to be considered similar
def is_close_enough(duration1, duration2):
return duration1 == duration2
# makes a copy of source file according to the renaming rules specified and puts the resulting file in the destination folder
def rename_by_rules(src_file, dst_dir, rules):
for k, rename_to in rules.items():
if is_close_enough(k, duration(src_file)):
shutil.copy2(src_file, os.path.join(dst_dir, rename_to))
# begins renaming your video files matched by duration according to the rules in your text file
rules = parse_rules('<replace with your text file>')
for base, dirs, files in os.walk('<replace with your source director>'):
for file in files:
if file.endswith('mp4'):
rename_by_rules(os.path.join(base, file), '<replace by dest dir>', rules)
My Python version is 3.6.1 and I have Windows 7 64bit. The traceback call is:
"F:\Dossier Gros Nain\Python\Python 3.6.1\python.exe" "F:/Dossier Gros Nain/Pluralsight courses/1h Fundamentals of Arnold for Maya/Videos dupli - Copie/supreme script of death.py"
Traceback (most recent call last):
File "F:/Dossier Gros Nain/Pluralsight courses/1h Fundamentals of Arnold for Maya/Videos dupli - Copie/supreme script of death.py", line 45, in <module>
rules)
File "F:/Dossier Gros Nain/Pluralsight courses/1h Fundamentals of Arnold for Maya/Videos dupli - Copie/supreme script of death.py", line 33, in rename_by_rules
if is_close_enough (k, duration (src_file)):
File "F:/Dossier Gros Nain/Pluralsight courses/1h Fundamentals of Arnold for Maya/Videos dupli - Copie/supreme script of death.py", line 20, in duration
return VideoFileClip (file).duration
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 81, in __init__
fps_source=fps_source)
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 68, in __init__
self.initialize()
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 103, in initialize
self.proc = sp.Popen(cmd, **popen_params)
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\subprocess.py", line 594, in __init__
_cleanup()
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\subprocess.py", line 205, in _cleanup
res = inst._internal_poll(_deadstate=sys.maxsize)
File "F:\Dossier Gros Nain\Python\Python 3.6.1\lib\subprocess.py", line 1025, in _internal_poll
if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] Descripteur non valide
Process finished with exit code 1
Is it a problem with movie.py? If yes, how could I remove it? Could I use ffprobe if nothing else works?