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

相关推荐
IOT那些事儿3 天前
Windows PowerShell配置Qt5编译运行环境
windows·powershell·qt5
今夕资源网5 天前
powershell工具包 安装升级脚本并设置UTF-8 环境快捷方式创建 将powershell的编码默认改为UTF-8
开发语言·utf-8·powershell·utf-8编码·powershell7·powershell5·设置utf-8编码
fengyehongWorld10 天前
Powershell 注册.dll类库到系统
powershell
就叫年华吧丶11 天前
Git Bash、CMD 与 PowerShell 的区别详解
linux·git·命令行·powershell·cmd·gitbash
Soari16 天前
Ziggo-CaaS-Switch软件配置教程
bash·powershell·tsn 交换机
LAM LAB22 天前
【shell】win10 Powershell字体美化
powershell
humors22124 天前
AI工具合集,不定期更新
人工智能·windows·ai·工具·powershell·deepseek
fengyehongWorld1 个月前
Powershell 自动安装Excel自定义标签页,开启宏
powershell
西西弗Sisyphus1 个月前
PowerShell 配置为启动时自动激活 Conda 环境
conda·powershell
夫唯不争,故无尤也2 个月前
curl与Invoke-RestMethod核心区别
后端·fastapi·powershell·curl