-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1080p_final_splitencoding.ps1
230 lines (196 loc) · 8.55 KB
/
1080p_final_splitencoding.ps1
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Reading the configuration file in JSON format
$config = Get-Content .\config.json | ConvertFrom-Json
Write-Host "Configuration loaded. FFMPEG path: $($config.FFMPEG)"
Write-Host "FFPROBE path: $($config.FFPROBE)"
Write-Host "Audio Conversion: $($config.CONVERT_AUDIO), Bitrate: $($config.AUDIO_BITRATE), Codec: $($config.AUDIO_CODEC)"
Write-Host "Video Conversion: $($config.CONVERT_VIDEO), Quality: $($config.VIDEO_QUALITY), Preset: $($config.VIDEO_PRESET)"
# Validation of FFMPEG and FFPROBE paths
if (-not (Test-Path $config.FFMPEG)) {
Write-Host "Invalid FFMPEG path. Check config."
exit
}
elseif (-not (Test-Path $config.FFPROBE)) {
Write-Host "Invalid FFPROBE path. Check config."
exit
}
function Get-ValidDirectory {
do {
$inputDirectory = Read-Host "Please enter a valid directory path"
$isValid = Test-Path $inputDirectory -PathType Container
if (-not $isValid) {
Write-Host "Invalid directory. Please try again."
}
} while (-not $isValid)
return $inputDirectory
}
function Get-VideoFiles {
param (
[string]$directory
)
return @("*.mp4", "*.mkv") | ForEach-Object { Get-ChildItem -Path $directory -Filter $_ -File }
}
function Extract-AudioTracks {
param (
[string]$inputFile
)
# Running ffprobe and capturing the output as JSON
$ffprobeOutput = & $($config.FFPROBE) -v error -select_streams a -show_entries "stream=index,codec_name,codec_type,bit_rate,channels" -of json $inputFile | Out-String
# Converting the JSON string to an object
$ffprobeObject = $ffprobeOutput | ConvertFrom-Json
# Extracting only the streams part and display
$tracks = $ffprobeObject.streams
#$tracks | Format-Table -AutoSize
return $tracks
}
function Extract-SubtitleTracks {
param (
[string]$inputFile
)
# Running ffprobe and capturing the output as JSON
$ffprobeOutput = & $($config.FFPROBE) -v error -select_streams s -show_entries "stream=index,codec_name,codec_type" -of json $inputFile | Out-String
# Converting the JSON string to an object
$ffprobeObject = $ffprobeOutput | ConvertFrom-Json
# Extracting only the streams part and display
$tracks = $ffprobeObject.streams
#$tracks | Format-Table -AutoSize
foreach ($track in $tracks) {
$subtitles = $subtitles + "-map 0:$($track.index) -c:$($track.index) copy "
}
Write-Host $subtitles
return $subtitles
}
function Process-AudioTracks {
param (
$tracksArray
)
foreach ($track in $tracksArray) {
if ($config.CONVERT_AUDIO -eq "true") {
if ($track.codec_name.Contains("dts") -or $track.codec_name.Contains("truehd")) {
if ($track.channels -gt 5) {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) 6 -b:$($track.index) $($config.AUDIO_BITRATE) "
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) $($track.channels) -b:$($track.index) $($config.AUDIO_BITRATE) "
}
Write-Host "DTS or TrueHD track found, preparing conversion command for track index $($trackid)"
} elseif ($track.codec_name.Contains("ac3")) {
if ([int]$track.bit_rate -gt 350000) {
if ($track.channels -gt 5) {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) 6 -b:$($track.index) $($config.AUDIO_BITRATE) "
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) $($track.channels) -b:$($track.index) $($config.AUDIO_BITRATE) "
}
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) copy "
}
} elseif ($track.codec_name.Contains("mp3") -or $track.codec_name.Contains("aac") -or $track.codec_name.Contains("flac")) {
if ([int]$track.bit_rate -gt 350000) {
if ($track.channels -gt 5) {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) 6 -b:$($track.index) $($config.AUDIO_BITRATE) "
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) $($config.AUDIO_CODEC) -ac:$($track.index) $($track.channels) -b:$($track.index) $($config.AUDIO_BITRATE) "
}
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) copy "
}
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) copy "
}
} else {
$command = $command + "-map 0:$($track.index) -c:$($track.index) copy "
}
}
Write-Host "Audio conversion command: $command"
return $command
}
function Process-File {
param (
[string]$inputFile
)
# Insert audio track extraction and FFmpeg command construction here
$audioTracks = Extract-AudioTracks $inputFile
$audioCommand = Process-AudioTracks $audioTracks
$subtitleTracks = Extract-SubtitleTracks $inputFile
if ($config.CONVERT_VIDEO -eq "true") {
$videoTrack = Extract-VideoTrack $inputFile
$videoCommand = Process-VideoTrack $videoTrack
}
$outputFile = Build-OutputFilename $inputFile
$ffmpegCommand = Build-FfmpegCommand -subtitles $subtitleTracks -audioCommand $audioCommand -videoCommand $videoCommand -inputFile $inputFile -outputFile $outputFile
Execute-FfmpegCommand $ffmpegCommand
Write-Host "Finished processing of $inputFile"
}
function Extract-VideoTrack {
param (
[string]$inputFile
)
Write-Host "Extracting video codec information..."
$videoCodec = & $($config.FFPROBE) -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 -i $inputFile
return $videoCodec
}
function Process-VideoTrack {
param (
[string]$videoCodec
)
switch -wildcard ($videoCodec) {
"*h264*" { $videoCodec = $($videoCodec + "_cuvid"); Write-Host "H264 video codec found." }
"*hevc*" { $videoCodec = $($videoCodec + "_cuvid"); Write-Host "HEVC video codec found." }
"*vc1*" { $videoCodec = $($videoCodec + "_cuvid"); Write-Host "VC-1 video codec found." }
}
Write-Host "Decode GPU acceleraction for codec: $videoCodec"
return $videoCodec
}
function Build-FfmpegCommand {
param (
[string]$audioCommand,
[string]$videoCommand,
[string]$inputFile,
[string]$outputFile,
[string]$subtitles
)
# Function logic goes here
# -loglevel error -stats
$command = " -y "
if ($videoCommand -ne "") {
$command = $command + "-hwaccel cuda -hwaccel_output_format cuda -c:V:0 $videoCommand "
}
$command = $command + "-i `"$inputFile`" -map 0:V -map_metadata 0 -c:d copy "
if ($config.CONVERT_VIDEO -eq "true") {
$command = $command + " -c:V:0 hevc_nvenc -cq $($config.VIDEO_QUALITY) -preset $($config.VIDEO_PRESET) -vf scale_cuda=format=p010le -split_encode_mode 2"
} else {
$command = $command + "-c:V:0 copy"
}
$command = "$command $audioCommand $subtitles"
$command = "$command`"$outputFile`" "
Write-Host $command
return $command
}
function Build-OutputFilename {
param (
[string]$inputFile
)
Write-Host "Preparing output file path..."
if ($inputFile.Contains("mp4")) {
$outputFile = $inputFile.Replace(".mp4", ".conv.mp4")
}
elseif ($inputFile.Contains("mkv")) {
$outputFile = $inputFile.Replace(".mkv", ".conv.mkv")
}
Write-Host "Output will be saved to: $outputFile"
return $outputFile
}
function Execute-FfmpegCommand {
param (
[string]$command
)
$arguments = $command -split ' '
& $($config.FFMPEG) $arguments
}
# Prompting for input directory
$inputDirectory = Get-ValidDirectory
# Listing files in the input directory
$files = Get-VideoFiles $inputDirectory
# Further script processing based on the files and configurations...
foreach ($file in $files) {
Write-Host "Processing file: $file"
Process-File $file.FullName
}