Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell Comcut.ps1 version #48

Open
TommyPaulkDell opened this issue May 18, 2023 · 2 comments
Open

Powershell Comcut.ps1 version #48

TommyPaulkDell opened this issue May 18, 2023 · 2 comments

Comments

@TommyPaulkDell
Copy link
Contributor

Decided to make a powershell version of comcut for fun (and for me):

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
)
$ldPath = $env:LD_LIBRARY_PATH
$env:LD_LIBRARY_PATH = ""
$exitcode = 0
if (-not $infile) {
$exename = Split-Path -Leaf $MyInvocation.MyCommand.Path
Write-Host "Remove commercial from 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 (-not $outfile) {
$outfile = $infile
}
$outdir = Split-Path $outfile -Parent
$outextension = $outfile -split "." | Select-Object -Last 1
$comskipoutput = ""
if ($workdir) {
if (!($workdir.EndsWith(""))) {
$comskipoutput = "--output=$workdir"
$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 - $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
}
& $ffmpegPath -hide_banner -loglevel error -nostdin -i $metafile -i ("concat:$($concat.Substring(1))") -c copy -map_metadata 0 -y $outfile
}
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
}

@TommyPaulkDell
Copy link
Contributor Author

TommyPaulkDell commented May 18, 2023

ComCutps1.txt

@BrettSheleski
Copy link
Owner

Awesome. Make a pull request and I'll consider adding it to the main repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants