summaryrefslogtreecommitdiffstats
path: root/win/C#/frmReadDVD.cs
blob: 2c95731df516e08630abb2c2f600ae3eb414daa1 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Handbrake
{
    public partial class frmReadDVD : Form
    {

        string inputFile;

        public frmReadDVD(string inputFile)
        {
            InitializeComponent();
            this.inputFile = inputFile;
        }

        public void scan(string filename)
        {
            string query = "-i " + '"' + filename + '"' + " -t0";
            System.Diagnostics.Process hbProc = new System.Diagnostics.Process();
            hbProc.StartInfo.FileName = "hbcli.exe";
            hbProc.StartInfo.RedirectStandardOutput = true;
            hbProc.StartInfo.RedirectStandardError = true;
            hbProc.StartInfo.Arguments = query;
            hbProc.StartInfo.UseShellExecute = false;
            hbProc.Start();
            System.IO.StreamReader errorReader = new System.IO.StreamReader(new System.IO.BufferedStream(hbProc.StandardError.BaseStream));
            hbProc.WaitForExit();
            hbProc.Close();

            //Parsing.DVD thisDvd = Parsing.DVD.Parse(errorReader);

            String DvdData = errorReader.ReadToEnd();
            DvdData = DvdData + "-- end --";

            String[] DvdDataArr = DvdData.Split('\n');
            int DvdDataSize = DvdDataArr.Length -1;
            String line = "";
            int counter = 0;

            //
            // Some varbiles used for parseing HandBrakes output
            //

            // DVD info stroage varibles
            string titleData  = "";
            string duationData  = "";
            string sizeData  = "";
            string cropdata  = "";
            string chatperData  = "";
            string audioData  = "";
            string subtitleData  = "";

            string fullTitleData  = "";

            // Position Pointers
            bool chapterPointer  = false;
            bool audioPointer = false;
            bool subtitlePointer = false;

            // Error handling varibles
            bool titleError = false;
            bool readError = false;
            
            while (counter <= DvdDataSize)
            {
                line = DvdDataArr[counter];
                counter++;
                 
                // Get all the 1 liner data and set chaper potiner to true when done
                if (line.Contains("exited.")){
                    subtitlePointer = false;
                    fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();
                    add(fullTitleData, titleData, duationData);
                    counter++;
                }else if (line.Contains("+ title")){
                    if (titleData != "") {
                        subtitlePointer = true;
                        fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();
                        add(fullTitleData, titleData, duationData);
                        counter = counter + 1;
                    }
                    titleData = line;
                }else if (line.Contains("+ duration")) {
                    duationData = line;
                }else if (line.Contains("+ size")) {
                    sizeData = line;
                }else if (line.Contains("+ autocrop")) {
                    cropdata = line;
                }else if (line.Contains("+ chapters")) {
                    chatperData = line;
                    chapterPointer = true;
                }

                // Get all the chapter information in 1 varible
                if (chapterPointer == true)
                {
                    if (!line.Contains("+ audio"))
                    {
                        chapterPointer = false;
                        audioPointer = true;
                        audioData = line;
                   }
                    else 
                    {
                        if (!chatperData.Equals(line))
                        {
                            chatperData = chatperData + " & " + line.Trim();
                        }
                    }
                }

                 // Get all the audio channel information in 1 varible
                if (audioPointer == true)
                { 
                    if (line.Contains("+ subtitle"))
                    {
                        audioPointer = false;
                        subtitlePointer = true;
                        subtitleData = line;
                    } 
                    else 
                    {
                        if (!audioData.Equals(line))
                        {
                            audioData = audioData + " & " + line.Trim();
                        }
                    }
                 }

                 //Get all the subtitle data into 1 varible
                 if (subtitlePointer == true)
                 {
                            if (line.Contains("+ subtitle")) 
                            {
                                subtitleData = line;
                            } else 
                            {
                                if (!subtitleData.Equals(line))
                                {
                                    subtitleData = subtitleData + " & " + line.Trim();
                                }
                            }
                  }

                  // Handle some of Handbrakes Error outputs if they occur.
                  if (line.Contains("No title"))
                  {
                    titleError = true;
                  }

                  if (line.Contains("***")) 
                  {
                    readError = true;
                  }

                 // Display error messages for errors detected above.
                 if (readError == true)
                 {
                     MessageBox.Show("Some DVD Title information may be missing however you may still be able to select your required title for encoding!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                 }

                 if (titleError == true)
                 {
                     MessageBox.Show("No Title(s) found. Please make sure you have selected a valid, non-copy protected source.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                 }

            }
        }

        public void add(string fullTitleData, string titleData, string durationData)
        {

            try
            {
                string t = titleData.Trim().Substring(8).Replace(":", "");
                string d = durationData.Trim().Substring(12);

                // Lets store the captured full title data as a string in the programs settings file.
                // This can then be used by the DVD title dropdown menu to populate other fields.

                Properties.Settings.Default.FullDVDInfo = Properties.Settings.Default.FullDVDInfo + " \n " + fullTitleData;

                //Now lets add the info to the main form dropdowns
                frmMain form = (frmMain)frmMain.ActiveForm;
                string title = t + " " + " " + d + " ";
                form.drp_dvdtitle.Items.Add(title);
            }
            catch (Exception)
            {
                // Don't really need to do anything about it.
            }
        }


        private void btn_ok_Click(object sender, EventArgs e)
        {
            scan(inputFile);
        }

       
    }
}