Created
May 16, 2023 19:17
-
-
Save schtritoff/8e0105060cb07c118863867429473830 to your computer and use it in GitHub Desktop.
Convert PySceneDetectCSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Convert PySceneDetect output CSV file to something that can be embedded in MKV or MP4 | |
.EXAMPLE | |
Frst create scenes file using PySceneDetect | |
"%~dp0tools\software\PySceneDetect\scenedetect.exe" -i "%input%" -s "%output_dir%\my_video.stats.csv" --min-scene-len 1s --merge-last-scene list-scenes -s -o "%output_dir%" detect-adaptive --threshold 7 --frame-window 100 save-images -o "%output_dir%" export-html | |
Then convert CSV to file format supported by ffmpeg or mp4box | |
for ffmpeg: | |
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File "%~dp0Convert-PySceneDetectCSV.ps1" -InputFile "%~dp0output_scenedetect\%filename%-Scenes.csv" -OutputFile "%~dp0output_scenedetect\%filename%-Scenes.ffmetadata.txt" -OutputType "ffmetadata" | |
"%~dp0tools\software\ffmpeg\FFmpeg\ffmpeg.exe" -loglevel error -hide_banner -nostdin -i "%~dp0output_nvenc\%filename%.%output_ext%" -f ffmetadata -i "%~dp0output_scenedetect\%filename%-Scenes.ffmetadata.txt" -map_metadata 1 -map_chapters 1 -c copy -y "%~dp0output_nvenc\%filename%_2.%output_ext%" | |
for mp4box | |
powershell.exe -NonInteractive -ExecutionPolicy ByPass -File "%~dp0Convert-PySceneDetectCSV.ps1" -InputFile "%~dp0output_scenedetect\%filename%-Scenes.csv" -OutputFile "%~dp0output_scenedetect\%filename%-Scenes.nerochap.txt" -OutputType "nero-vorbis-txt" | |
"%~dp0tools\software\GPAC\mp4box.exe" -add "%~dp0output_scenedetect\%filename%-Scenes.nerochap.txt:chap" "%~dp0output_nvenc\%filename%.%output_ext%" | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True)] | |
[ValidateNotNullOrEmpty()] | |
[string]$InputFile, | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[string]$OutputFile, | |
# ffmpeg metadata https://ffmpeg.org/ffmpeg-formats.html#Metadata-1 | |
# nero,apple,matroska examples https://github.com/rigaya/NVEnc/blob/master/NVEncC_Options.en.md#--chapter-string | |
# chapter metadata in detail for audio https://github.com/Zeugma440/atldotnet/wiki/Focus-on-Chapter-metadata | |
[ValidateSet('ffmetadata','nero-vorbis-txt')] | |
[string]$OutputType = "ffmetadata" | |
) | |
# import PySceneDetect CSV file | |
$chapters = Import-Csv -Path $InputFile | |
# create empty output txt file | |
Set-Content -Path $OutputFile -Value '' -NoNewline -Force | |
# write header if needed for file format | |
switch ($OutputType) | |
{ | |
"ffmetadata" { Add-Content -Path $OutputFile -Value ';FFMETADATA1' } | |
} | |
# write chapters | |
foreach ($c in $chapters) { | |
Write-Verbose $c | |
## @{Scene Number=1; Start Frame=1; Start Timecode=00:00:00.000; Start Time (seconds)=0.000; End Frame=458; End Timecode=00:00:18.320; End Time (seconds)=18.320; Length (frames)=458; Length (timecode)=00:00:18.320; Length (seconds)=18.320} | |
# Write-Host $c.'Scene Number' | |
switch ($OutputType) { | |
"nero-vorbis-txt" { | |
<# | |
CHAPTER001=00:00:00.000 | |
CHAPTER001NAME=Chapter 1 | |
CHAPTER002=00:05:00.000 | |
CHAPTER002NAME=Chapter 2 | |
#> | |
Add-Content -Path $OutputFile -Value @" | |
CHAPTER$('{0:d2}' -f [int]$c.'Scene Number')=$($c.'Start Timecode') | |
CHAPTER$('{0:d2}' -f [int]$c.'Scene Number')NAME=Chapter $($c.'Scene Number') | |
"@ | |
} | |
"ffmetadata" { | |
<# | |
https://ffmpeg.org/ffmpeg-formats.html#Metadata-1 | |
[CHAPTER] | |
TIMEBASE=1/1000 | |
START=0 | |
#chapter ends at 0:01:00 | |
END=60000 | |
title=chapter \#1 | |
#> | |
Add-Content -Path $OutputFile -Value @" | |
[CHAPTER] | |
TIMEBASE=1/1000 | |
# Chapter $('{0:d2}' -f [int]$c.'Scene Number') starts at $($c.'Start Timecode') | |
START=$( [int]$c.'Start Time (seconds)' * 1000 ) | |
# Chapter $('{0:d2}' -f [int]$c.'Scene Number') ends at $($c.'End Timecode') | |
END=$( [int]$c.'End Time (seconds)' * 1000 ) | |
title=CHAPTER $('{0:d2}' -f [int]$c.'Scene Number') | |
"@ | |
} | |
} | |
} | |
# Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment