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

相关推荐
DogDaoDao2 天前
从零开始:Windows 系统中 PowerShell 配置 FFmpeg 的详细步骤
windows·ffmpeg·音视频·ffplay·powershell·视频直播·ffprobe
三巧1 个月前
判断按键盘是否好使的开机自启动PowerShell脚本
计算机外设·powershell
一夜空中最亮的星一2 个月前
【Windows】PowerShell 缓存区大小调节
windows·缓存·powershell·ps·windows 终端
安冬的码畜日常2 个月前
【工欲善其事】利用 DeepSeek 实现复杂 Git 操作:从原项目剥离出子版本树并同步到新的代码库中
git·ai·powershell·cherry-pick·deepseek·deepseek r1·deepseek v3
Eastmount2 个月前
[论文阅读] (36)C&S22 MPSAutodetect:基于自编码器的恶意Powershell脚本检测模型
论文阅读·系统安全·powershell·自编码器·恶意代码检测
H轨迹H3 个月前
一文学会powshell使用及功能
网络安全·渗透测试·powershell·红队全栈
tiandyoin3 个月前
删除 C 盘空文件夹--递归删除
脚本·batch·powershell·空文件夹
JUNIOR_MU4 个月前
【VMware VCF】管理 VCF 环境中组件的密码策略。
powershell·vcf·vmware cloud foundation·powervcf·password management·password policy·password expiration·password complexity·account lockout
Ztiddler4 个月前
【CMD、PowerShell和Bash设置代理】
开发语言·深度学习·github·bash·powershell·代理·cmd