' ' lechap.vbs "@(#)lechap.vbs 1.2 2006/01/10 snoops.de" ' Let's Edit DVC file chapter extraction and export ' ' Usage: lechap [file.dvc] ' A dialog with options is shown if no file is given as argument ' The file can also be dragged and dropped onto this script file ' Chapters are written to a text file selected in a dialog, with ' the default name set as the input file with a .txt extension ' Use at your own risk, no warranty of any kind! ' Henry ' Option Explicit Dim DEBUG Dim sTitle Dim sVersion Dim oShell Dim oDialog Dim sSourceFile Dim sOutputFile Dim sBinary Dim vDVC Dim iDVCmajor Dim iDVCminor Dim iDVChz Dim sDVCtv Dim iDVCsize Dim iDVCskip Dim iDVCchap Dim iDVCdone Dim i Dim iNull Dim aDVC() Dim bIEwait Dim DVCchtime Dim DVCchtimeff Dim DVCchtimeffsep Dim DVCchhex Dim DVCchsep ' set DEBUG to 1 for verbose chapter recognition DEBUG = 0 sTitle = "Let" & Chr(39) & "s EDIT DVC Chapter Extract and Export" sVersion = "lechap.vbs 1.2" ' the first 2 bytes of dvc file contain the version ' LE2 uses 99.29 (hex values) ' LE1 uses 97.29 and has no chapter info iDVCmajor = &H99 iDVCminor = &H29 ' check for file argument or show dialog with complete options Set oShell = Wscript.CreateObject ("Wscript.Shell") If Wscript.Arguments.Count > 0 Then sSourceFile = Wscript.Arguments(0) DVCchtime = True DVCchtimeff = True DVCchtimeffsep = ":" DVCchhex = False DVCchsep = vbNewLine Else sSourceFile = DVCdialog(sTitle) If sSourceFile = "" Then Wscript.Quit End If ' set default output file name sOutputFile = sSourceFile & ".txt" ' read file in binary mode, get size and size modulus 4 Const adTypeBinary = 1 Set sBinary = CreateObject("ADODB.Stream") With sBinary .Type = adTypeBinary .Open .LoadFromFile sSourceFile vDVC = .Read iDVCsize = .Size iDVCskip = iDVCsize Mod 4 .Close End With If iDVCsize < 4 Then msgbox "Error in " & sSourceFile & vbCrLf & _ "File is corrupt, size in bytes is only " & iDVCsize, _ vbCritical + vbSystemModal, sTitle Wscript.Quit End If ' check if dvc file is a supported version, but allow to continue If Ascb(Midb(vDVC, 1, 1)) <> iDVCmajor Or Ascb(Midb(vDVC, 2, 1)) <> iDVCminor Then i = msgbox("Error in " & sSourceFile & vbCrLf & _ "File version is not " & Hex(iDVCmajor) & "." & Hex(iDVCminor) & vbCrLf & _ "File version data is " & Hex(Ascb(Midb(vDVC, 1, 1))) & "." & Hex(Ascb(Midb(vDVC, 2, 1))) & _ vbCrLf & vbCrLf & "Do you really want to continue?", _ vbYesNo + vbCritical + vbDefaultButton2 + vbSystemModal, sTitle) If i <> vbYes Then Wscript.Quit End If ' size the dynamic chapter array based on the size of file chosen ReDim aDVC(CLng(iDVCsize \ 4)) ' my chapter scan logic, with no documentation on dvc file structure: ' scan file data backwards, in 4-byte words (chapters are at the end) ' look for the matching chapter count that precedes the chapter data ' a chapter is the frame number on the timeline, first frame is null ' one chapter could be set at the first frame (null chapter value) ' so quit if two null values are detected before chapter recognition iDVCchap = 0 iDVCdone = 0 iNull = 0 For i = (iDVCsize - 4) To iDVCskip Step -4 Dim iB1 Dim iB2 Dim iB3 Dim iB4 iB1 = Ascb(Midb(vDVC, (i + 1), 1)) iB2 = 256 * Ascb(Midb(vDVC, (i + 2), 1)) iB3 = 65536 * Ascb(Midb(vDVC, (i + 3), 1)) iB4 = 16777216 * Ascb(Midb(vDVC, (i + 4), 1)) aDVC(iDVCchap) = iB1 + iB2 + iB3 + iB4 If DEBUG Then msgbox "value " & iDVCchap & " = " & aDVC(iDVCchap) & _ " (0x" & Hex(aDVC(iDVCchap)) & ")", vbSystemModal, sTitle End If If aDVC(iDVCchap) = 0 Then iNull = iNull + 1 ElseIf aDVC(iDVCchap) = iDVCchap Then iDVCdone = 1 Exit For End If If iNull > 1 Then Exit For End If iDVCchap = iDVCchap + 1 Next If iDVCdone = 0 Then Dim sTMP sTMP = "Sorry, cannot find any chapters in " & sSourceFile If iNull > 1 Then sTMP = sTMP & vbCrLf & "Two null values without chapter recognition" End If msgbox sTMP, vbInformation + vbSystemModal, sTitle Wscript.Quit End If ' get the tv standard from the dvc file ' tv byte is followed by 8 bytes before the chapter count and data ' this logic only works if the file has chapters i = (iDVCsize - (iDVCchap * 4)) - 12 iDVChz = Ascb(Midb(vDVC, i, 1)) Select Case iDVChz Case 50 sDVCtv = "PAL" iDVChz = 25 Case 60 sDVCtv = "NTSC" iDVChz = 29.97 Case Else i = msgbox("Problem with " & sSourceFile & vbCrLf & _ "Unknown TV standard value 0x" & Hex(iDVChz) & vbCrLf & vbCrLf & _ "Assume PAL? (Choose No for NTSC)", vbYesNoCancel + vbCritical + vbSystemModal, sTitle) If i = vbCancel Then Wscript.Quit sDVCtv = "PAL" iDVChz = 25 If i <> vbYes Then sDVCtv = "NTSC" iDVChz = 29.97 End If End Select ' feedback before dialog msgbox "Selected file: " & sSourceFile & vbCrLf & _ vbTab & "TV standard: " & sDVCtv & vbCrLf & _ vbTab & "Chapter count: " & iDVCchap & vbCrLf & vbCrLf & _ "A dialog will allow choosing the text file for chapter export", vbSystemModal, sTitle ' provide a dialog to save a text file with the chapters ' use selected chapter format, output in original order Set oDialog = CreateObject("SAFRCFileDlg.FileSave") oDialog.FileName = sOutputFile oDialog.FileType = "Text file" If oDialog.OpenFileSaveDlg Then Dim oFSO Dim oFile Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFile = oFSO.CreateTextFile(oDialog.FileName) For i = (iDVCchap - 1) To 0 Step -1 If DVCchtime Then Dim iHH : Dim iMM : Dim iSS Dim iTime : Dim iTimeRem iHH = (60 * 60 * iDVChz) iMM = (60 * iDVChz) iSS = iDVChz iTime = aDVC(i) \ iHH iTimeRem = aDVC(i) Mod iHH If iTime < 10 Then oFile.Write "0" oFile.Write iTime & ":" iTime = iTimeRem \ iMM iTimeRem = iTimeRem Mod iMM If iTime < 10 Then oFile.Write "0" oFile.Write iTime & ":" iTime = iTimeRem \ iSS iTimeRem = iTimeRem Mod iSS If iTime < 10 Then oFile.Write "0" oFile.Write iTime If DVCchtimeff Then oFile.Write DVCchtimeffsep If iTimeRem < 10 Then oFile.Write "0" oFile.Write iTimeRem End If ElseIf DVCchhex Then oFile.Write Hex(aDVC(i)) Else oFile.Write aDVC(i) End If If i <> 0 Then oFile.Write DVCchsep Next oFile.Close Else msgbox "Chapter export was aborted. Chapter data was not saved", vbInformation + vbSystemModal, sTitle End If Wscript.Quit ' dialog to browse for a file and set chapter output options ' this function returns the full name of the file chosen ' other choices in the dialog are simply placed in global variables ' use html dialog due to lacking form functions in native vbs Function DVCdialog(sTitle) Dim oIE Const cWidth = 650 : Const cHeight = 500 set oIE = CreateObject("InternetExplorer.Application") With oIE .Width = cWidth .Height = cHeight .Offline = True .FullScreen = False .Navigate("about:blank") .AddressBar = False .MenuBar = False .Silent = True .StatusBar = False .ToolBar = False While .Busy : WScript.Sleep 100 : Wend With .document With .ParentWindow .moveto (.screen.width - cWidth) \ 2, (.screen.height - cHeight) \ 2 End With .Title = sTitle .WriteLn("" & sTitle & "") .WriteLn("

" & sTitle & "

") .WriteLn("

DVC file  ") .WriteLn("

") .WriteLn("

Chapter export format") .WriteLn("


") .WriteLn(" Time") .WriteLn("") .WriteLn("Frame field separator ") .WriteLn("
") .WriteLn(" Do not include frame field") .WriteLn("

") .WriteLn(" Frame") .WriteLn("") .WriteLn(" Hexadecimal frame numbers") .WriteLn("

") .WriteLn("

Chapter separation ") .WriteLn("") .WriteLn("


") .WriteLn("

" & sVersion & " snoops.de") .all.ok.onclick = getref("DVCsubmit") .all.dvc.focus oIE.Visible = True bIEwait = True On Error Resume Next Do While bIEwait WScript.Sleep 100 If Err Then Exit Do If oIE.Visible = False Then Wscript.Quit Loop DVCchtime = .all.chtype(0).checked DVCchtimeff = Not .all.chtimeff.checked DVCchtimeffsep = .all.chtimeffsep.selectedIndex Select Case DVCchtimeffsep Case 0 DVCchtimeffsep = ":" Case 1 DVCchtimeffsep = "." Case 2 DVCchtimeffsep = ";" End Select DVCchhex = .all.chhex.checked DVCchsep = .all.chsep.selectedIndex Select Case DVCchsep Case 0 DVCchsep = "," Case 1 DVCchsep = vbNewLine Case 2 DVCchsep = " " Case 3 DVCchsep = vbTab End Select DVCdialog = .all.dvc.value On Error GoTo 0 End With ' .document .Visible = False .Quit End With ' oIE End Function Sub DVCsubmit() bIEwait = False End Sub ' EOF