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

相关推荐
龙泉寺天下行走6 天前
[powershell 入门]第9天:PowerShell 安全、代码签名与企业部署 作业及深度解析
安全·powershell
課代表7 天前
bat 批处理文件中 PowerShell 命令换行问题
符号·参数·powershell·批处理·换行·续行符·管道符
龙泉寺天下行走8 天前
[Powershell 入门教程]第10天:综合项目实战与进阶
powershell
龙泉寺天下行走13 天前
[powershell 入门教程]第9天:PowerShell 安全、代码签名与企业部署
安全·powershell
課代表14 天前
PowerShell 字符转 UniCode 编码
字符编码·类型转换·unicode·powershell·批处理·转义·[char]
fengyehongWorld16 天前
Powershell 实现系统静音
powershell
艾莉丝努力练剑17 天前
【Git:企业级开发模型】Git企业级Git工作流实战:基于Git Flow的分支模型与开发流程
服务器·git·ubuntu·gitee·centos·powershell·企业级开发模型
charlee4424 天前
Git使用经验总结8-Git仓库历史记录清除
git·powershell·敏感信息·仓库重置·历史重写
tq10861 个月前
一个简单的 PowerShell REPL 脚本
powershell
guojikun2 个月前
一键配置 Web 前端开发环境(PowerShell 自动化脚本)
windows·web前端·powershell