I have a lot of videos all stored in different subfolders. All that videos have weird file names like "f4vd5sd1b5dsd41s415d"
. I also 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 my text file looks like this:
name of video 1
length of video 1
name of video 2
length of video 2
...
I tried to make a script that compares the length of the video with the random file name with the file length of my text file, and if they match the title is added ( so one line backwards ) to the video in question.
To better explain it here's some work I did manually:
Before
After
Here's my scripting attempt, but I think it's incomplete:
# all imports
from moviepy.editor import VideoFileClip
import os
import shutil
#to copy all files that are in multiple subfolder into one subfolder
src = r'"my\\dir\\is\\here\\with\\all\\videos"'
dst = "my\\dir\\is\\here"
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
if filename == filename in dst:
os.rename(filename,filename+"123") #trying to prevent that equal filename receives another filename but does not work
shutil.copy2 (filename, dst)
#to search all files and print file names
for root, dirs, files in os.walk (
"my\\dir\\is\\here"):
for f in files:
if f.endswith ("mp4"):
f_name, f_ext = os.path.splitext (f)
print (f_name)
if f.endswith ("mp4"):
f = VideoFileClip (f) #does not work because movie.py doesn't go through subfolders like os.walk and spits an error
dur = f.duration
print (format (dur / 60, '.0f') + 'm' + ' ' + format (dur % 60, '.0f') + 's') #to convert it in min and sec
#to open and read txt file
with open ('2.txt') as fo:
print (fo.read ())
#count lines of txt datei
with open("2.txt") as foo:
lines = len(foo.readlines())
#if -schleife when dur of txt datei == dur of clip.duration with modulo operator(maybe works?)
if lines % dur:
print(lines-1 ) # to say that every second line to match with the dur and if the dur and lines are equal move file
Most of the code shown here is from solutions from other posts on StackOverflow. I am stuck writing the remainder of this script.