Skip to content

Instantly share code, notes, and snippets.

@PureOcean
Last active March 16, 2022 16:09
Show Gist options
  • Save PureOcean/254f5c594f9aebb21238797dc23d950c to your computer and use it in GitHub Desktop.
Save PureOcean/254f5c594f9aebb21238797dc23d950c to your computer and use it in GitHub Desktop.
Converts cut edits of VirtualDub to the syntax supported by MKVMerge.
; VirtualDub's processing settings (VCF) and VirtualDub2's Project
; files store user edits using like:
;
; VirtualDub.subset.AddRange(Frame, FrameCount)
;
; This AutoIt script forked of the VCF2AVS coded by Darksoul71
; (https://forum.doom9.org/showthread.php?p=774386#post774386)
;
; Changed to read cut segments only in .VDProject & .VCF files.
;
; Exports a TXT file that will be cut frames intervals in VirtualDub
; Project file for MKVMerge.
;
; The exported frame intervals to TXT are used with the --split parts-frames
; switch of MKVMerge.
;
; So, the segments of videos that are cutted and trimmed in VDub can be
; extract without re-encoding, thanks to MKVMerge. Even If HEVC videos.
; Of course, the starting and ending frames numbers must be the keyframes.
;
; Usage:
; VDProject2MKVMerge.exe VDProject=MyVDubEdits.VDProject TXT=FramesForMKVmerge.txt
;
; Check out the output FramesForMKVmerge.txt. Frame intervals will be in here.
;++++++++++++++++++++++++++++++++++++++++++++++++++
Func IsCLIParam($Switch, $Param)
$SwitchLen = StringLen($Switch)
If ($SwitchLen > StringLen($Param)) Then Return (0)
$Temp = StringLeft ($Param,$SwitchLen)
If ($Switch = $Temp) Then
Return (1)
Else
Return (0)
EndIf
EndFunc
;++++++++++++++++++++++++++++++++++++++++++++++++++
Func GetCLIParam($Switch, $Param)
$SwitchLen = StringLen($Switch)
If ($SwitchLen > StringLen($Param)) Then Return ("")
Return(StringTrimLeft ($Param,$SwitchLen))
EndFunc
;++++++++++++++++++++++++++++++++++++++++++++++++++
Func ParseCLI (ByRef $VDProject, ByRef $TXT)
; Local Init
$VDProject = ""
$TXT = ""
; Obtain number of commandline parameters
$ArgCount = $CmdLine[0]
; Return NoSuccess if
If ($ArgCount = 0) Then Return (0)
For $i = 1 To $ArgCount
If (IsCLIParam("VDProject=", $CmdLine[$i]) <> 0) Then $VDProject = GetCLIParam("VDProject=", $CmdLine[$i])
If (IsCLIParam("TXT=", $CmdLine[$i]) <> 0) Then $TXT = GetCLIParam("TXT=", $CmdLine[$i])
Next
Return (1)
EndFunc
;++++++++++++++++++++++++++++++++++++++++++++++++++
Func GenerateCutList ($VDProject)
$MaxCuts = 4000
Dim $CutMarks[$MaxCuts]
For $i = 0 To $MaxCuts - 1
$CutMarks[$i] = ""
Next
$CutList = ""
$Count = 0
$file = FileOpen($VDProject, 0)
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
If StringInStr($line,"VirtualDub.subset.AddRange(") Then
$Temp = StringSplit (StringTrimRight(StringReplace($Line,"VirtualDub.subset.AddRange(",""),2),",")
$FirstFrame = $Temp[1]
$LastFrame = $Temp[1] + $Temp[2] - 1
$CutMarks[$Count] = "" & $FirstFrame & "-" & $LastFrame & ""
$Count = $Count + 1
EndIf
Wend
FileClose($file)
For $i = 0 To $Count - 1
If ($CutMarks[$i] <> "") Then
; Generate Cutlist without fading
If ($CutList <> "") Then $CutList = $CutList & ",+" & $CutMarks[$i]
If ($CutList = "") Then $CutList = $CutMarks[$i]
Else
ExitLoop
EndIf
Next
Return ($CutList)
EndFunc
;++++++++++++++++++++++++++++++++++++++++++++++++++
Func AddLine ($TextFile, $Line)
$file = FileOpen($TextFile, 1)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
FileWriteLine($file, $Line)
FileClose($file)
EndFunc
;++++++++++++++++++++++++++++++++++++++++++++++++++
; Variables Init
$VDProject = ""
$TXT = ""
If (ParseCLI ($VDProject, $TXT) = 0) Then
$VDProject = FileOpenDialog("Choose VDProject file" , "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "VirtualDub Project/Config (*.VDProject)", 1)
$TXT = FileOpenDialog("Choose AVISynth script" , "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", "AVISynth Script (*.txt)", 2, StringTrimRight ($VDProject,0) & ".txt")
EndIf
; If TXT is missing extension then add it
If (StringUpper(StringRight($TXT,4)) <> ".txt") Then $TXT = $TXT & ".txt"
$CutList = GenerateCutList ($VDProject)
If ($Cutlist <> "") Then
AddLine ($TXT, $Cutlist)
EndIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment