-
Notifications
You must be signed in to change notification settings - Fork 26
/
ComChap.ps1
144 lines (144 loc) · 4.74 KB
/
ComChap.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
param(
[string]$infile,
[string]$outfile,
[switch]$keepEdl,
[switch]$keepMeta,
[string]$ffmpegPath = ".\ffmpeg.exe",
[string]$comskipPath = ".\comskip.exe",
[string]$comskipini = "$env:USERPROFILE\.comskip.ini",
[string]$lockfile,
[string]$workdir
)
if (-not $infile) {
$exename = Split-Path -Leaf $MyInvocation.MyCommand.Path
Write-Host "Add chapters to video file using EDL file"
Write-Host " (If no EDL file is found, comskip will be used to generate one)"
Write-Host ""
Write-Host "Usage: $exename infile [outfile]"
exit 1
}
[bool]$deleteedl = !$keepEdl
[bool]$deletemeta = !$keepMeta
$deletelog = $true
$deletelogo = $true
$deletetxt = $true
$tempfiles = @()
$totalcutduration = 0
If ($lockfile) {
if (Test-Path $lockfile) {
Write-Host "lockfile: $lockfile"
Write-Host "Waiting" -ForegroundColor Yellow
while (Test-Path $lockfile) {
sleep 5
Write-Host "." -NoNewLine -ForegroundColor Yellow
}
}
New-Item $lockfile
}
if (-not $outfile) {
$outfile = $infile
}
$outdir = Split-Path $outfile -Parent
$outextension = $outfile -split "\." | Select-Object -Last 1
$comskipoutput = ""
if ($workdir) {
if (!($workdir.EndsWith("\"))) {
$workdir += "\"
}
$comskipoutput = "$workdir"
$infileb = Split-Path $infile -Leaf
$edlfile = "${workdir}$($infileb -replace '\.[^.]+$').edl"
$metafile = "${workdir}$($infileb -replace '\.[^.]+$').ffmeta"
$logfile = "${workdir}$($infileb -replace '\.[^.]+$').log"
$logofile = "${workdir}$($infileb -replace '\.[^.]+$').logo.txt"
$txtfile = "${workdir}$($infileb -replace '\.[^.]+$').txt"
}
else {
$edlfile = "$($infile -replace '\.[^.]+$').edl"
$metafile = "$($infile -replace '\.[^.]+$').ffmeta"
$logfile = "$($infile -replace '\.[^.]+$').log"
$logofile = "$($infile -replace '\.[^.]+$').logo.txt"
$txtfile = "$($infile -replace '\.[^.]+$').txt"
}
if (-not (Test-Path $comskipIni)) {
"output_edl=1" | Out-File -Encoding utf8 $comskipIni
}
elseif ((Get-Content $comskipIni) -notcontains "output_edl=1") {
"output_edl=1" | Add-Content -Encoding utf8 $comskipIni
}
if (-not (Test-Path $edlfile)) {
& "$comskipPath" $comskipoutput --ini="$comskipIni" "$infile"
}
$start = 0
$i = 0
$hascommercials = $false
$concat = ""
$lines = Get-Content $edlfile
foreach ($line in $lines) {
$fields = $line -split "`t"
$end = [float]$fields[0]
$startnext = [float]$fields[1]
if ([double]$end * 1000 -gt [double]$start * 1000) {
$i++
$hascommercials = $true
Add-Content -Path $metafile ";FFMETADATA1"
Add-Content -Path $metafile "[CHAPTER]"
Add-Content -Path $metafile "TIMEBASE=1/1000"
Add-Content -Path $metafile "START=$([int]($start * 1000 - $totalcutduration * 1000))"
Add-Content -Path $metafile "END=$([int]($end * 1000 - $totalcutduration * 1000))"
Add-Content -Path $metafile "title=Chapter $i"
$chapterfile = "$($infile -replace '\.[^.]+$').part-$i.ts"
if ($workdir) {
$chapterfile = Split-Path $chapterfile -Leaf
$chapterfile = "$workdir$chapterfile"
}
$tempfiles += $chapterfile
$concat += "|$chapterfile"
$duration = [double]$end - [double]$start
& $ffmpegPath -hide_banner -loglevel error -nostdin -i $infile -ss $start -t $duration -c copy -y $chapterfile
$totalcutduration += $startnext - $end
}
$start = $startnext
}
if ($hascommercials) {
$endstring = & $ffmpegPath -hide_banner -nostdin -i $infile 2>&1 | Select-String -Pattern "Duration" | ForEach-Object { $_ -replace '\D+(\d+:\d+:\d+.\d+),.*', '$1' }
$end=([TimeSpan]::Parse($endstring)).TotalSeconds
if ([double]$end * 1000 -gt [double]$start * 1000) {
$i++
Add-Content -Path $metafile "[CHAPTER]"
Add-Content -Path $metafile "TIMEBASE=1/1000"
Add-Content -Path $metafile "START=$([int]($start * 1000))"
Add-Content -Path $metafile "END=$([int]($end * 1000))"
Add-Content -Path $metafile "title=Chapter $i"
}
try {& $ffmpegPath -hide_banner -loglevel error -nostdin -i $infile -i $metafile -c copy -map_metadata 1 -y $outfile}
catch {Write-Host "Error running ffmpeg: $infile"}
}
If (!(Test-Path $outfile)) {
Write-Error "Error, $outfile does not exist."
Exit 1
}
foreach ($tempfile in $tempfiles) {
Remove-Item -Path $tempfile
}
if ($deleteedl) {
Remove-Item -Path $edlfile
}
if ($deletemeta) {
Remove-Item -Path $metafile
}
if ($deletelog) {
Remove-Item -Path $logfile
}
if ($deletelogo) {
Remove-Item -Path $logofile
}
if ($deletetxt) {
Remove-Item -Path $txtfile
}
if ($ldPath) {
$env:LD_LIBRARY_PATH = $ldPath
}
if ($lockfile) {
Remove-Item -Path $lockfile
}