Share

目录下视频根据分辨率分组

import os
import subprocess
import shutil


def get_resolution(file_path):
    # 使用FFmpeg获取视频分辨率
    result = subprocess.Popen(
        [
            "ffprobe",
            "-v",
            "error",
            "-select_streams",
            "v:0",
            "-show_entries",
            "stream=width,height",
            "-of",
            "csv=s=x:p=0",
            file_path,
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output, _ = result.communicate()
    resolution = output.decode("utf-8").strip()
    return resolution


def organize_videos_by_resolution(directory):
    # 遍历目录下的所有文件
    for filename in os.listdir(directory):
        if filename.endswith((".mp4", ".avi", ".mkv", ".mov", ".flv")):  # 可根据需要添加其他视频格式
            file_path = os.path.join(directory, filename)
            resolution = get_resolution(file_path)

            # 创建目录并移动文件
            resolution_directory = os.path.join(directory, resolution)
            os.makedirs(resolution_directory, exist_ok=True)
            shutil.move(file_path, os.path.join(resolution_directory, filename))


if __name__ == "__main__":
    target_directory = "/Users/gd/Desktop/未命名文件夹"  # 替换为你的视频目录路径
    organize_videos_by_resolution(target_directory)