-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnormalize.ps1
More file actions
142 lines (136 loc) · 7.8 KB
/
normalize.ps1
File metadata and controls
142 lines (136 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
param (
[string]$RankNum = [Math]::Floor(
((Get-Date).ToFileTime() / 10000000 - 11644473600 - 1277009809 + 133009) / 3600 / 24 / 7),
[array]$Part = @('*')
)
$ProgressPreference = 'SilentlyContinue'
$TruePath = Split-Path $MyInvocation.MyCommand.Path
$DownloadFolder = "$($TruePath)/ranking/list0"
$FootageFolder = "$($TruePath)/ranking/list1"
$tmp = Start-Process -NoNewWindow -Wait -PassThru -FilePath 'ffmpeg.exe' -ArgumentList '-loglevel error -f lavfi -i color=black:s=1920x1080 -vframes 1 -an -c:v h264_nvenc -f null -' -RedirectStandardError '.\NUL'
if ($tmp.ExitCode -eq 0 ) { $Nvdia = $true } else { $Nvdia = $false }
$LostVideos = @()
(Get-Content "$($TruePath)/LostFile.json" | ConvertFrom-Json).psobject.Properties.Name | ForEach-Object {
$LostVideos += $_
}
function Normailze {
param (
[parameter(position = 1)]$Rank,
[parameter(position = 2)]$FileName,
[parameter(position = 3)]$Offset,
[parameter(position = 4)]$Length
)
$Rank = $Rank.ToString().PadLeft(2, '0')
if ($LostVideos -contains $FileName) {
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($FileName) 视频已失效,生成占位视频"
$FakeArg = -join @(
'-n -hide_banner -t 40 -f lavfi -i anullsrc -f lavfi '
'-i color=size=1280x720:duration=60:rate=60:color=AntiqueWhite '
"-vf drawtext=fontfile=MiSans-Medium.ttf:fontsize=147:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='$($FileName)' "
"$($FootageFolder)/$($Rank)_$($FileName).mp4"
)
Start-Process -NoNewWindow -Wait -FilePath 'ffmpeg.exe' -ArgumentList $FakeArg
return $null
}
$Target = 'loudnorm=I=-23.0:LRA=+7.0:tp=-1.0'
$Length = $Length + 5
$AudioArg = "-y -hide_banner -ss $($Offset) -t $($Length) -i $($DownloadFolder)/$($FileName).mp4 -af $($Target):print_format=json -f null -"
$AudioInfo = "$($DownloadFolder)/$($FileName).log"
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - 分析 $($FileName) 音频数据" -ForegroundColor Green
Start-Process -NoNewWindow -Wait -FilePath 'ffmpeg.exe' -RedirectStandardError $AudioInfo -ArgumentList $AudioArg
$AudioData = [Regex]::Match((Get-Content -Raw $AudioInfo), '(?s)({.+?})\r?\n').Value | ConvertFrom-Json
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($AudioData)"
$Source = "measured_I=$($AudioData.input_i):measured_LRA=$($AudioData.input_lra):measured_tp=$($AudioData.input_tp):measured_thresh=$($AudioData.input_thresh):offset=$($AudioData.target_offset)"
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($Source)"
if ($Nvdia) {
# Nvidia CUDA
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - 使用 Nvidia CUDA 加速转码"
$VideoArg = -join @(
"-y -hide_banner -loglevel error -ss $($Offset) -t $($Length) -i $($DownloadFolder)/$($FileName).mp4 "
"-vf scale='ceil((min(1,gt(iw,1920)+gt(ih,1080))*(gte(a,1920/1080)*1920+lt(a,1920/1080)*((1080*iw)/ih))+not(min(1,gt(iw,1920)+gt(ih,1080)))*iw)/2)*2:ceil((min(1,gt(iw,1920)+gt(ih,1080))*(lte(a,1920/1080)*1080+gt(a,1920/1080)*((1920*ih)/iw))+not(min(1,gt(iw,1920)+gt(ih,1080)))*ih)/2)*2' "
"-af $($Target):print_format=summary:linear=true:$($Source) -ar 48000 "
"-c:v h264_nvenc -b:v 10M -c:a aac -b:a 320k -r 60 $($FootageFolder)/$($Rank)_$($FileName).mp4"
)
} else {
# x264
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - 使用 CPU x264 转码"
$VideoArg = -join @(
"-y -hide_banner -loglevel error -ss $($Offset) -t $($Length) -i $($DownloadFolder)/$($FileName).mp4 "
"-vf scale='ceil((min(1,gt(iw,1920)+gt(ih,1080))*(gte(a,1920/1080)*1920+lt(a,1920/1080)*((1080*iw)/ih))+not(min(1,gt(iw,1920)+gt(ih,1080)))*iw)/2)*2:ceil((min(1,gt(iw,1920)+gt(ih,1080))*(lte(a,1920/1080)*1080+gt(a,1920/1080)*((1920*ih)/iw))+not(min(1,gt(iw,1920)+gt(ih,1080)))*ih)/2)*2' "
"-af $($Target):print_format=summary:linear=true:$($Source) -ar 48000 "
"-c:v libx264 -b:v 10M -c:a aac -b:a 320k -r 60 $($FootageFolder)/$($Rank)_$($FileName).mp4"
)
}
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - 截取视频并标准化音频" -ForegroundColor Green
Start-Process -NoNewWindow -Wait -FilePath 'ffmpeg.exe' -ArgumentList $VideoArg
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($FileName) 操作完成`n" -ForegroundColor Green
}
function EDNormalize {
param (
[parameter(position = 1)]$FileName
)
$Target = 'loudnorm=I=-23.0:LRA=+7.0:tp=-1.0'
$AudioArg = "-y -hide_banner -i ""./ranking/2_ed/$($FileName)"" -af $($Target):print_format=json -f null -"
$AudioInfo = './ranking/2_ed/ed.log'
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - 分析 $($FileName) 音频数据"
Start-Process -NoNewWindow -Wait -FilePath 'ffmpeg.exe' -RedirectStandardError $AudioInfo -ArgumentList $AudioArg
$AudioData = [Regex]::Match((Get-Content -Raw $AudioInfo), '(?s)({.+?})\r?\n').Value | ConvertFrom-Json
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($AudioData)"
$Source = "measured_I=$($AudioData.input_i):measured_LRA=$($AudioData.input_lra):measured_tp=$($AudioData.input_tp):measured_thresh=$($AudioData.input_thresh):offset=$($AudioData.target_offset)"
Write-Debug "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($Source)"
$EncodeArg = -join @(
"-y -hide_banner -loglevel error -i ""./ranking/2_ed/$($FileName)"" "
'-i "./ranking/2_ed/Cover.jpg" -map 0:0 -map 1:0 '
'-id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" '
"-af $($Target):print_format=summary:linear=true:$($Source) -ar 48000 "
'-c:a libmp3lame -q:a 0 "./ranking/2_ed/ed.mp3"'
)
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($FileName) 音频标准化" -ForegroundColor Green
Start-Process -NoNewWindow -Wait -FilePath 'ffmpeg.exe' -ArgumentList $EncodeArg
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($FileName) 操作完成`n" -ForegroundColor Green
}
function Main {
Import-Module powershell-yaml
$Files = @()
$LocalVideos = @()
$RankVideos = @()
if ($Part.Contains('*')) {
$Files = Get-Content -Raw "$($FootageFolder)/$($RankNum)_*.yml"
Get-ChildItem "$($FootageFolder)/*.mp4" | ForEach-Object { $LocalVideos += $_.BaseName }
} else {
$Part | ForEach-Object {
$Files += Get-Content -Raw "$($FootageFolder)/$($RankNum)_$($_).yml"
}
}
$Files | ForEach-Object {
ConvertFrom-Yaml $_ | ForEach-Object {
$_ | ForEach-Object {
$RankVideos += @{r = $_.':rank'; n = $_.':name'; o = $_.':offset'; l = $_.':length' }
}
}
}
$RankVideos | ForEach-Object {
if ($Part.Contains('*')) {
$f = "$($_.r.ToString().PadLeft(2, '0'))_$($_.n)"
if (($LocalVideos -notcontains $f) -or ((Get-Item "$($FootageFolder)/$($f).mp4").length -eq 0)) {
Normailze $_.r $_.n $_.o $_.l # -Debug
} else {
Write-Host "$(Get-Date -Format 'MM/dd HH:mm:ss') - $($_.n) 已存在,跳过处理" -ForegroundColor Green
}
} else {
Normailze $_.r $_.n $_.o $_.l -Debug
}
}
Add-Type -AssemblyName Microsoft.VisualBasic
Get-ChildItem "$($DownloadFolder)/*.log" | ForEach-Object {
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile(
"$($_)", 'OnlyErrorDialogs', 'SendToRecycleBin')
}
$EDFile = Get-ChildItem -Path './ranking/2_ed/*' -Include *.mp3, *.flac | Where-Object Name -NotMatch 'ed.mp3' | Select-Object -ExpandProperty Name
if ($null -ne $EDFile ) {
EDNormalize $EDFile
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile(
"$($TruePath)/ranking/2_ed/$($EDFile)", 'OnlyErrorDialogs', 'SendToRecycleBin')
}
}
Main