我有很多视频存储在不同的子文件夹中。所有视频都有奇怪的文件名,如f4vd5sd1b5dsd41s415d
。我有一个文本文件,其中有每个文件的正确文件名。要知道哪个视频与哪个标题相关,我添加了要与标题对应的视频的文件持续时间,因此在我的文本文件中有:
:视频名称1
第2行中的:视频1的视频长度
第3行中的:视频2的名称
第4行中的:视频2的视频长度
为了更好地解释这里,我手动完成了一些工作:
之前:
后:
我的脚本是:
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)
我的Python版本是3.6.1,我有Windows 7 64位。回溯调用是:
"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
movie.py有问题吗?如果是,我怎么能删除它?如果没有别的办法可以使用ffprobe吗?