精易论坛

标题: 求助大哥 文件内删除指定文件的操作 [打印本页]

作者: longdaqing    时间: 2025-4-19 16:11
标题: 求助大哥 文件内删除指定文件的操作
各位大佬好,请问下大家,一个文件内,我都很多的视频文件,是mp4格式的,然后视频的时长有10s到2分钟多的都有,我现在想删除这个文件夹内视频时长小于30秒的视频,低于30s的视频都删掉,30s以上的视频就保留,请问下大哥们这个应该怎么弄呢?


文件像下面截图这样的:


作者: Ruang    时间: 2025-4-19 16:11
自行调试
demo.e (846.32 KB, 下载次数: 6)
作者: Ruang    时间: 2025-4-19 16:47
有问题么
作者: BOR    时间: 2025-4-19 17:54
批量多线程处理 需要+QQ 23582530 收费的
作者: BOR    时间: 2025-4-19 17:54
+23582530  
作者: lufanjingyi    时间: 2025-4-19 18:28
先枚举出文件数组   然后循环读取MP4的时长判断删除
作者: 155能不能    时间: 2025-4-19 18:45
很多方法可以实现
作者: 承易    时间: 2025-4-19 20:15
[Python] 纯文本查看 复制代码
import os
from moviepy.editor import VideoFileClip

def delete_short_videos(folder_path, min_duration=30):
    """
    删除文件夹中时长小于min_duration秒的MP4视频
   
    参数:
        folder_path (str): 要处理的文件夹路径
        min_duration (int): 最小保留时长(秒),默认为30秒
    """
    deleted_count = 0
    kept_count = 0
   
    for filename in os.listdir(folder_path):
        if filename.lower().endswith('.mp4'):
            filepath = os.path.join(folder_path, filename)
            
            try:
                # 获取视频时长
                with VideoFileClip(filepath) as video:
                    duration = video.duration
               
                # 判断并删除
                if duration < min_duration:
                    os.remove(filepath)
                    print(f"已删除: {filename} (时长: {duration:.2f}秒)")
                    deleted_count += 1
                else:
                    print(f"保留: {filename} (时长: {duration:.2f}秒)")
                    kept_count += 1
                    
            except Exception as e:
                print(f"处理文件 {filename} 时出错: {str(e)}")
   
    print(f"\n处理完成: 删除了 {deleted_count} 个文件, 保留了 {kept_count} 个文件")

# 使用示例
folder_path = input("请输入要处理的文件夹路径: ")
delete_short_videos(folder_path)

作者: 承易    时间: 2025-4-19 20:15
[Python] 纯文本查看 复制代码
import os
import subprocess

def get_video_duration_ffprobe(filepath):
    """使用ffprobe获取视频时长(秒)"""
    cmd = [
        'ffprobe', '-v', 'error', '-show_entries',
        'format=duration', '-of',
        'default=noprint_wrappers=1:nokey=1', filepath
    ]
    try:
        output = subprocess.check_output(cmd).decode('utf-8').strip()
        return float(output)
    except:
        return None

def delete_short_videos_ffprobe(folder_path, min_duration=30):
    """
    使用ffprobe删除短视频
   
    参数:
        folder_path (str): 要处理的文件夹路径
        min_duration (int): 最小保留时长(秒),默认为30秒
    """
    deleted_count = 0
    kept_count = 0
   
    for filename in os.listdir(folder_path):
        if filename.lower().endswith('.mp4'):
            filepath = os.path.join(folder_path, filename)
            duration = get_video_duration_ffprobe(filepath)
            
            if duration is None:
                print(f"无法获取 {filename} 的时长,跳过")
                continue
               
            if duration < min_duration:
                os.remove(filepath)
                print(f"已删除: {filename} (时长: {duration:.2f}秒)")
                deleted_count += 1
            else:
                print(f"保留: {filename} (时长: {duration:.2f}秒)")
                kept_count += 1
   
    print(f"\n处理完成: 删除了 {deleted_count} 个文件, 保留了 {kept_count} 个文件")

# 使用示例
folder_path = input("请输入要处理的文件夹路径: ")
delete_short_videos_ffprobe(folder_path)

作者: 走一回    时间: 2025-4-19 22:24
  
子程序名返回值类型公开备 注
_按钮1_被单击  
变量名类 型静态数组备 注
all_mp4文本型0
i  
cmd文本型 
time_整数型 
' ffprobe.exe 有68MB,太大了,请自行下载到   mp4目录  下
文件_枚举 (“d:\mp4目录”, “*.mp4”, all_mp4, 真, , )
变量循环首 (取数组成员数 (all_mp4), 1, -1, i)
cmd = “d:\mp4目录\ffprobe.exe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 ” + all_mp4 [i]
time_ = 到整数 (系统_取DOS执行结果 (cmd, ))
如果真 (time_ < 30)
调试输出 (time_, “删除”)

变量循环尾 ()


i支持库列表   支持库注释   
spec特殊功能支持库








欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4