import os import shutil import subprocess import concurrent.futures from pathlib import Path from datetime import datetime # 指定ffmpeg的二进制文件夹位置,如果ffmpeg不在系统PATH中 FFMPEG_BIN = "C:\\ffmpeg-7.0.2-full_build\\bin\\" # 请根据实际情况修改路径 def log_error(message): """将错误消息写入到日志文件中""" error_log_file = f"errorlog-{datetime.now().strftime('%Y%m%d')}.txt" with open(error_log_file, 'a') as f: timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') f.write(f"[{timestamp}] {message}\n") def get_video_resolution_ffprobe(video_path): """使用ffprobe获取视频文件的分辨率""" ffprobe_cmd = os.path.join(FFMPEG_BIN, 'ffprobe') if FFMPEG_BIN else 'ffprobe' cmd = [ ffprobe_cmd, '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=p=0', video_path ] try: output = subprocess.check_output(cmd).decode().strip() width, height = map(int, output.split(',')) return width, height except subprocess.CalledProcessError as e: error_message = f"ffprobe error for file {video_path}: {e.output.decode()}" log_error(error_message) return None, None def move_video(video_path, base_dir): """移动单个视频文件""" try: # 获取视频分辨率 _, height = get_video_resolution_ffprobe(video_path) if height: # 创建目标目录 target_dir = Path(base_dir).parent / str(height) target_dir.mkdir(exist_ok=True) # 移动文件 target_path = target_dir / Path(video_path).name shutil.move(video_path, target_path) print(f"Moved '{video_path}' to '{target_path}'") except Exception as e: error_message = f"Error moving file {Path(video_path).name}: {e}" log_error(error_message) def move_videos_by_height(base_dir): """根据视频高度并行移动文件""" video_files = [] for root, _, files in os.walk(base_dir): for file in files: if file.lower().endswith(('.mp4', '.mkv', '.avi', '.mov', '.wmv', '.rmvb', '.flv', '.m4v', '.mpg')): video_files.append(os.path.join(root, file)) with concurrent.futures.ThreadPoolExecutor() as executor: # 使用多线程处理文件 executor.map(move_video, video_files, [base_dir] * len(video_files)) # 用法示例 base_directory = "foo:\\bar" move_videos_by_height(base_directory)