使用 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

相关推荐
无为之士11 小时前
Windows 批量打印 PDF 工具分享:支持文件夹、指定文件、当天文件、预览列表
windows·powershell
阆遤8 天前
Windows环境安装Hermes Desktop安装
powershell·ai agent·hermes desktop
DONSEE广东东信智能读卡器25 天前
用PowerShell实现Windows 本地 WSS/HTTPS 自签名证书配置方法
windows·网络协议·https·powershell·身份证阅读器
DogDaoDao1 个月前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch
console.log('npc')1 个月前
Windows 11 安装 WSL2 + Ubuntu + Docker + Codex + Sub2API 教学
windows·docker·powershell·ubantu·codex
CV工程师(Ctrl)1 个月前
Codex 在 Windows 中文路径执行bash命令编码问题复盘
powershell·codex
vortex51 个月前
PowerShell 的命令补全方案: PSReadLine + PSCompletions + argc + Carapace
windows·powershell
深念Y1 个月前
踩坑实录:把 Windows 默认 PowerShell 换成 7.x 到底有多坑?一条龙解决指南
windows·乱码·bug·控制台·powershell·管道·流式
IOT那些事儿2 个月前
Windows PowerShell配置Qt5编译运行环境
windows·powershell·qt5
今夕资源网2 个月前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码