使用 PowerShell 脚本 + FFmpeg 在 Windows 系统中批量计算 MP4视频 文件的总时长

步骤 1:安装 FFmpeg

访问 FFmpeg 官网(Download FFmpeg),下载 Windows 版编译包(如 ffmpeg-release-full.7z)。或者到(https://download.csdn.net/download/zjx2388/90539014)下载完整资料

解压文件,将 ffmpeg.exe 和 ffprobe.exe 所在的 bin 目录路径(例如 C:\ffmpeg\bin)添加到系统环境变量中。

打开命令提示符,输入 ffprobe -version 验证是否安装成功。

步骤 2:编写 PowerShell 脚本

保存以下代码为 Get-TotalVideoDuration.ps1(右键新建文本文件,粘贴后修改后缀为 .ps1):

指定视频文件夹路径(例如:C:\Videos)

$videoFolder = "C:\Your\Video\Folder"

$totalSeconds = 0

获取所有 MP4 文件

videoFiles = Get-ChildItem -Path videoFolder -Filter *.mp4 -Recurse

foreach (file in videoFiles) {

使用 ffprobe 获取时长(单位:秒)

duration = ffprobe -v error -show_entries format=duration -of csv=p=0 file.FullName

累加总时长

totalSeconds += \[float\]duration

输出单文件时长(可选)

timeSpan = \[TimeSpan\]::FromSeconds(duration)

Write-Host "File (file.Name): (timeSpan.ToString('hh\:mm\:ss'))"

}

计算总时长并格式化

totalTime = \[TimeSpan\]::FromSeconds(totalSeconds)

Write-Host "`n--------------------------------"

Write-Host "Total Duration: (totalTime.ToString('hh\:mm\:ss'))"

步骤 3:运行脚本

右键 Get-TotalVideoDuration.ps1,选择 使用 PowerShell 运行。

若提示"脚本执行被禁止",需先以管理员身份运行 PowerShell 并执行:

Set-ExecutionPolicy RemoteSigned

相关推荐
winfredzhang3 天前
用 wxPython 打造「Claude Code 任务启动器」:一个把 AI CLI 变成一键工作流的桌面工具
python·wxpython·powershell·剪贴板·claudecodecli
鬼手点金4 天前
Hermes‑Agent 使用教程
python·ubuntu·powershell·cmd·ai agent·opencode·hermes
寒水馨10 天前
macOS下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-osx-arm64.pkg)
macos·自动化·跨平台·shell·脚本·命令行·powershell
寒水馨10 天前
Linux下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-linux-x64.tar.gz)
linux·跨平台·自动化运维·devops·命令行·powershell·脚本语言
儒雅的烤地瓜1 个月前
计算机网络 | 网络诊断双刃剑:Tracert路由追踪实战与Windows防火墙端口配置指南
tcp/ip·计算机网络·wireshark·防火墙·powershell·cmd
vortex51 个月前
PowerShell Provider 机制详解:从文件系统到注册表的统一抽象
windows·powershell
无为之士2 个月前
Windows 批量打印 PDF 工具分享:支持文件夹、指定文件、当天文件、预览列表
windows·powershell
阆遤2 个月前
Windows环境安装Hermes Desktop安装
powershell·ai agent·hermes desktop
DONSEE广东东信智能读卡器3 个月前
用PowerShell实现Windows 本地 WSS/HTTPS 自签名证书配置方法
windows·网络协议·https·powershell·身份证阅读器
DogDaoDao3 个月前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch