[Asm] 纯文本查看 复制代码 $files = Get-ChildItem *.mp4,*.mkv
foreach ($file in $files) {
# 定义函数以获取视频时长
function Get-VideoDuration($file) {
$ffmpegOutput = & ffmpeg -i $file 2>&1
$durationMatch = [regex]::Match($ffmpegOutput, 'Duration: (\d+:\d+:\d+\.\d+)')
if ($durationMatch.Success) {
$duration = $durationMatch.Groups[1].Value
$timeComponents = $duration -split ':'
$durationInSeconds = [int]$timeComponents[0] * 3600 + [int]$timeComponents[1] * 60 + [math]::Round([double]$timeComponents[2])
return $durationInSeconds
}
return 0
}
#文件
$fileRootPath = $file.Name
#$fileRootPath = Read-Host "名称带文件类型"
#$fileRootPath = ""
$time = Get-VideoDuration $fileRootPath
#Write-Host "视频时长:$time 秒"
# 获取视频时长
$durationInSeconds = Get-VideoDuration $fileRootPath
# 计算截取的时间点,从第10秒开始,平均分成9帧
$frameTimes = @()
for ($i = 0; $i -lt 9; $i++) {
$frameTimes += [math]::Round(10 + $durationInSeconds / 9 * $i)
}
# 定义文件路径和参数
$outputPath = "img"
New-Item -Path ./$outputPath -ItemType Directory -Force
# 生成创建图像的命令 -n 是跳过 -y是覆盖
for ($i = 0; $i -lt 9; $i++) {
$command = "ffmpeg -ss " + $frameTimes[$i] + " -i `"$fileRootPath`" -vframes 1 -q:v 2 " + $outputPath + "\frame_$i.jpg"
Invoke-Expression $command
Write-Host "已截取图像 $i"
}
# 合并截取的图像成图像网格
# 获取文件大小
$fileInfo = Get-Item $file.Name
$fileSizeBytes = $fileInfo.Length
$fileSizeGB = [math]::Round($fileSizeBytes / 1GB, 2) # 转换为千兆字节
# 转换时间
$hours = [math]::floor($time / 3600) # 计算小时
$minutes = [math]::floor(($time % 3600) / 60) # 计算分钟
$seconds = $time % 60 # 计算剩余的秒数
$名称 =$file.Name -replace ".mp4|.mkv", ""
# 水印图像文件路径
$watermarkImage = "D:/Software/O.png"
#$watermarkImage = "E:/O.png"
# 合并图像和水印
$font = "D:/Software/ios.ttf"
#$font = "E:/ios.ttf"
$text = "名 称:$名称`n视频时长:$hours 小时 $minutes 分钟 $seconds 秒`n大 小:$fileSizeGB.GB"
$标题 = "$名称"
$视频时长 = "视频时长:$hours 小时 $minutes 分钟 $seconds 秒"
$大小 = "大小:$fileSizeGB.GB"
$outputFileName = "$fileRootPath.jpg"
$mergeCommand = "ffmpeg -y -i `"$outputPath\frame_%d.jpg`" -i $watermarkImage -filter_complex `"
[0:v]scale=-1:468,drawbox=x=0:y=0:w=iw:h=ih:c=white:t=10,tile=3x3,
pad=iw:ih+155:0:155:white,drawtext=text='$text':fontfile='$font':fontsize=35:fontcolor=black:x=600:y=30[bg];
[bg][1:v]overlay=10:10`" `"$outputFileName`""
Invoke-Expression $mergeCommand
Remove-Item -Path .\$outputPath -Recurse #删除图片
Write-Host "图像网格创建完成。"
}
鸡肋 |