summaryrefslogtreecommitdiffstats
path: root/win/Handbrake/frmReadDVD.vb
blob: b5a5cf1a3f8acc2dde83795a668e1c004fdb2148 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Imports System.IO
Imports System
Imports System.Diagnostics
Imports System.Threading

Public Class frmReadDVD

    '#
    '# Start Reading the DVD as soon as the window launches
    '# Write the output to dvdinfo.dat
    '#
    Private Sub frmStatus_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        Dim ApplicationPath As String = Application.StartupPath

        Try
            Shell("cmd /c """"" + ApplicationPath + "\hbcli.exe"" -i """ + frmMain.text_source.Text + """" & " -t0 >" + """" + ApplicationPath + "\dvdinfo.dat""" + " 2>&1""")
        Catch ex As Exception
            MessageBox.Show("ERROR: There was a problem launching the encoder. Code: frmS-1")
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    '#
    '#
    '# Ok Button Handler
    '# Stage 1 - Wait til hbcli.exe has finished writing data out to file dvdinfo.dat
    '# Stage 2 - Parse the dvdinfo.dat file
    '# Stage 3 - output the parsed version to dvd.dat or handle errors if any
    '#
    '#

    Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
        '# Stage 1 - Wait til the hbcli exe has finished
        Dim isRunning As Integer
        Dim process2 As Process = New Process
        Dim running As Boolean = True
        Dim hbProcess As Process() = Process.GetProcesses()

        While running
            Thread.Sleep(1000)
            hbProcess = Process.GetProcesses()
            running = False
            Dim processArr2 As Process() = hbProcess
            Dim i As Integer = 0
            While i < CInt(processArr2.Length)
                Dim process1 As Process = processArr2(i)
                If process1.ProcessName.Equals("hbcli") Then
                    running = True
                End If
                i = i + 1
            End While
        End While
        isRunning = 0


        '# Stage 2 - Parse the dvdinfo.dat file
        '# This involves creating a string for each title and putting it into an array.
        '# This array is called dvdData()


        ' Lets clean any previous contents out of the DVD Title dropdown.
        frmMain.drp_dvdtitle.Items.Clear()

        Dim file_path As String = Application.StartupPath
        Dim errStatus As Integer = 0
        Dim dvdData(150) As String
        Dim break As Boolean = False
        Dim titleError As Boolean = False

        Try
            ' Parse the Data into a Single String with ~ Sepeartor
            Dim RlineFile As StreamReader = File.OpenText(file_path & "\dvdinfo.dat")
            Dim RLine As String
            Dim titleData As String = ""
            Dim ChaptStatus As Integer = 0
            Dim AudioTrackStatus As Integer = 0
            Dim SubtitleStatus As Integer = 0
            Dim counter As Integer = 0
            Dim counter2 As String = 0
            Dim ErrorCounter As Integer = 0
            RLine = "---"
            dvdData(counter2) = "---Start---"

            While RLine <> Nothing
                If (RLine.Contains("+ title")) Then
                    If (titleData <> "") Then
                        dvdData(counter2) = titleData
                        add(titleData)
                        counter2 = counter2 + 1
                    End If
                    ChaptStatus = 0
                    AudioTrackStatus = 0
                    SubtitleStatus = 0
                    titleData = RLine.Trim
                ElseIf (RLine.Contains("exited.")) Then
                    add(titleData)
                    dvdData(counter2) = titleData
                    counter2 = counter2 + 1
                    ChaptStatus = 0
                    AudioTrackStatus = 0
                    SubtitleStatus = 0
                    break = True
                ElseIf (RLine.Contains("***")) Then
                    errStatus = 1
                ElseIf (RLine.Contains("No title")) Then
                    titleError = True
                    break = True
                ElseIf (RLine.Contains("+ duration")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                ElseIf (RLine.Contains("+ size")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                ElseIf (RLine.Contains("+ autocrop")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                ElseIf (RLine.Contains("+ chapters")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                    ChaptStatus = 1
                    AudioTrackStatus = 0
                    SubtitleStatus = 0
                ElseIf (RLine.Contains("+ audio")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                    ChaptStatus = 0
                    AudioTrackStatus = 1
                    SubtitleStatus = 0
                ElseIf (RLine.Contains("+ subtitle tracks")) Then
                    titleData = titleData & " ~ " & RLine.Trim
                    ChaptStatus = 0
                    AudioTrackStatus = 0
                    SubtitleStatus = 1

                ElseIf (ChaptStatus = 1) Then
                    ' This IF statment is here incase no chapters appear.
                    If (RLine.Contains("+ audio")) Then
                        ChaptStatus = 0
                        AudioTrackStatus = 1
                        SubtitleStatus = 0
                    End If
                    titleData = titleData & " & " & RLine.Trim

                ElseIf (AudioTrackStatus = 1) Then
                    'This if statment is here incase there was no audio tracks
                    If (RLine.Contains("+ subtitle tracks")) Then
                        ChaptStatus = 0
                        AudioTrackStatus = 1
                        SubtitleStatus = 0
                    End If
                    titleData = titleData & " & " & RLine.Trim

                ElseIf (SubtitleStatus = 1) Then
                    If (RLine.Contains("+ title")) Then
                        If (titleData <> "") Then
                            dvdData(counter2) = titleData
                            counter2 = counter2 + 1
                        End If
                        ChaptStatus = 0
                        AudioTrackStatus = 0
                        SubtitleStatus = 0
                        titleData = RLine.Trim
                    Else
                        titleData = titleData & " & " & RLine.Trim
                    End If
                End If
                RLine = RlineFile.ReadLine()

                If break = True Then
                    RLine = Nothing
                ElseIf RLine = "" Then
                    RLine = " "
                    ErrorCounter = ErrorCounter + 1
                    If ErrorCounter = 50 Then
                        RLine = Nothing
                    End If
                End If

                counter = counter + 1
            End While
            '# Close the file. Its no longer needed here.
            RlineFile.Close()

        Catch ex As Exception
            MessageBox.Show(ex.ToString) ' Debug
        End Try

        '# Stage 4 - Write the parsed data out into a file. 
        '# But, if theres a problem, display an error message instead
        If errStatus = 1 Then
            MessageBox.Show("ERROR: Error Reading the DVD. Some Title information may not be available.")
        End If

        If titleError = True Then
            MessageBox.Show("ERROR: No Title(s) found.")
        End If

        Try
            Dim DataWriter As StreamWriter = New StreamWriter(file_path & "\dvd.dat")
            Dim dvdCount As Integer = dvdData.Length
            Dim counter As String = 0

            While counter <> dvdCount
                DataWriter.WriteLine(dvdData(counter))
                counter = counter + 1
            End While
            DataWriter.Close()

        Catch ex As Exception
            MessageBox.Show("ERROR: Unable to write Data file. Please make sure the application has admin privileges.")
        End Try

        Me.Close()
    End Sub

    '# A function to Add data to frmSelects Select Title Tab and also populate frmMains dvdTitle Tab
    Function add(ByVal titleData)
        Dim titleInfo() As String
        Dim str(7) As String
        Dim data As String = ""

        titleInfo = titleData.Split("~")
        Try
            str(0) = titleInfo(0).Trim.Substring(8).Replace(":", "") 'Title
            str(1) = titleInfo(1).Trim.Substring(12) ' Duration
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            MessageBox.Show("ERROR: Incomplete DVD data found. Please copy the data on the View DVD Information tab and report this error.")
        End Try

        'Now lets add the info to the main form dropdowns
        frmMain.drp_dvdtitle.Items.Add(str(0) & " (" & str(1) & ")")

        Return 0
    End Function

End Class