Code
Imports
As usual, I list the necessary Imports first:
Imports System.Runtime.InteropServices 'used for APIs
Imports System 'System imports
Imports System.IO 'File input / output
Imports System.Text 'Advanced Text capabilities
Declarations:
'Gets Drive Names
Private Declare Function GetLogicalDriveStrings Lib "kernel32.dll" _
Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Int32, _
ByVal lpBuffer As String) As Int32
'Checks for free space on disk
Declare Auto Function GetDiskFreeSpace Lib "kernel32.dll" ( _
ByVal lpRootPathName As String, _
ByRef lpSectorsPerCluster As UInt32, _
ByRef lpBytesPerSector As UInt32, _
ByRef lpNumberOfFreeClusters As UInt32, _
ByRef lpTotalNumberOfClusters As UInt32) As Integer
'Determines drive Type
Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" ( _
ByVal nDrive As String) As Int32
Private Const DRIVE_CDROM As Long = 5 'DVD / CD ROM constant
Private strDriveLetter As String 'Stores Drive Letter
Private strFinalOutput As String 'Stores Final Output Location
Private strFWOE As String 'Stores Filename Without Path & Extension
Private strOutExt As String 'Stores Selected Output File Format
Private strOutAudio As String 'Stores Audio Bitrate
Private strVidSize As String 'Stores Video Size
Private intQuality As Integer 'Stores Output Quality
Private blnDVD As Boolean 'Has A DVD Been Inserted
Private prcFFMPEG As New Process 'FFMPEG Process Object
ConvertFile function
Private Function ConvertFile() 'Function To Convert File
Control.CheckForIllegalCrossThreadCalls = False 'Disable Illegal Crossthread Calls From Controls
Dim strOutput As String 'Output File Name
Dim strFFMPEGOut As String 'Lines Read From Input / Source File
Dim strSource As String = txtSource.Text 'Source
Dim strFFMPEGCmd As String = " -i """ & strSource & """ -ar 2250 -b 64k -r 24 -y """ & strOutput & """"
Dim psiProcInfo As New System.Diagnostics.ProcessStartInfo 'Proc Info Object For FFMPEG.EXE
Dim srFFMPEG As StreamReader 'Reads Source File's Lines
' Dim cmd As String = " -i """ & input & """ -ar " & strOutAudio & " -b 64k -r 24 -s " & strVidSize & "-qscale " & intQuality & " -y """ & output & """" 'ffmpeg commands -y replace
If strFinalOutput <> "" And strFWOE <> "" And strOutExt <> "" And strOutAudio <> "" And strVidSize <> "" Then
strOutput = strFinalOutput & strFWOE & strOutExt
Else
MessageBox.Show("Ensure all settings are properly made!") 'If Something Not Set
Exit Function
End If
psiProcInfo.FileName = Application.StartupPath + "\ffmpeg.exe" 'Location Of FFMPEG.EXE
psiProcInfo.Arguments = strFFMPEGCmd 'Command String
psiProcInfo.UseShellExecute = False
psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden
psiProcInfo.RedirectStandardError = True
psiProcInfo.RedirectStandardOutput = True
psiProcInfo.CreateNoWindow = True
prcFFMPEG.StartInfo = psiProcInfo
prcFFMPEG.Start() 'Start Process
Me.Text = "Converting..."
srFFMPEG = prcFFMPEG.StandardError 'Enable Error Checking For FFMPEG.EXE
Me.btnStart.Enabled = False
Do
If bgwConvert.CancellationPending Then 'Cancelled?
Exit Function
End If
strFFMPEGOut = srFFMPEG.ReadLine 'Read Source File Line By Line
Loop Until prcFFMPEG.HasExited And strFFMPEGOut = Nothing Or strFFMPEGOut = "" 'Read Until There Is Nothing Left
Me.Text = "Done!"
Me.btnStart.Enabled = True
Return 0
End Function
The above function launches ffmpeg.exe with specific arguments. We make use
of a BackgroundWorker here because some files are huge and will take some time
to finish the conversion. We also do not want our program to freeze do we? You
have to remember that not all settings can be applied with all the various
output file types. You have to read the ffmpeg documentation properly to see
which setting works where. This line (which has been commented out):
' Dim cmd As String = " -i """ & input & """ -ar " & strOutAudio & " -b 64k -r 24 -s " & strVidSize & "-qscale " & intQuality & " -y """ & output & """" 'ffmpeg commands -y replace
Will mostly work only with AVI output files. So make sure your settings are compatible with your output.
GetDVDRomDriveLetter Function
'Function Determines DVD / CD ROM Drive Letter
Private Function GetDVDRomDriveLetter() As String
Dim lngDriveType As Long 'Holds Drive Type
Dim lngCurrentDrive As Long 'Current Drive Counter
Dim strDrive As String 'Holds Final Drive Letter
lngCurrentDrive = 1 'First Drive
Dim strDrives As String 'Holds All Drives
strDrives = Space(150) 'Ensure There Is Enough Space To hold All Drive Letters
Dim lRetVal As Long 'GetLogicalDriveStrings Retun Value
lRetVal = GetLogicalDriveStrings(150, strDrives) 'Get All Drive Strings
If lRetVal = 0 Then 'If GetLogicalDriveStrings() Failed, Reset Variables & Exit
GetDVDRomDriveLetter = vbNullString
Exit Function
End If
strDrive = Mid(strDrives, lngCurrentDrive, 3) 'Get Current Drive's Letter
Do
lngDriveType = GetDriveType(strDrive) 'Determine Drive Type Of Current Drive
If lngDriveType = DRIVE_CDROM Then 'If Type Is DVD / CD Store That Drive Letter
GetDVDRomDriveLetter = strDrive
Exit Function
End If
lngCurrentDrive = lngCurrentDrive + 4 'Increment Counter
strDrive = Mid(strDrives, lngCurrentDrive, 3) 'Get Drive Letter
Loop While (Mid(strDrives, lngCurrentDrive, 1) <> vbNullChar) 'Continue While Drive String Is Valid
End Function
With this function, we determine our DVD / CD ROM's drive letter.
GetDVDRomInfo Sub
Private Sub GetDVDRomInfo() 'Sub To Check If Disk Has Any Content
strDriveLetter = GetDVDRomDriveLetter() 'Use Correct Drive Letter For DVD / CD ROM
If (GetDiskFreeSpace(strDriveLetter, 0, _
0, 0, 0) > 0) Then 'If Disk Has Content
txtSource.Text = strDriveLetter 'Add Drive Letter To Source Textbox
txtVolumeLabel.Text = Dir(strDriveLetter, FileAttribute.Volume) 'Add Volume Label
ReadDVDRomFiles() 'Read DVD Contents
blnDVD = True 'Set DVD Boolean To True
Else 'Disk Is Empty
txtSource.Text = ""
txtVolumeLabel.Text = ""
blnDVD = False
End If
End Sub
Here we make use of the correct drive letter to determine the content of the
inserted disk.
ReadDVDRomFiles Sub
Private Sub ReadDVDRomFiles() 'Reads DVD / CD Contents
Dim diFolder As New IO.DirectoryInfo(strDriveLetter & "Video_Ts".ToUpper()) 'Determines If VIDEO_TS Folder Is Present
Dim arrFIFiles As IO.FileInfo() = diFolder.GetFiles("*.vob") 'List All .VOB Files
Dim fiFile As IO.FileInfo
For Each fiFile In arrFIFiles
lstFiles.Items.Add(fiFile.Name)
Next
End Sub
If the disk was not empty, we determine if any playable DVD content could be
found.
LoadFiles Sub
Private Sub LoadFiles() 'Add Files From Selected Folder
Dim FilePath As String
For Each FilePath In Directory.GetFiles(fbdSource.SelectedPath)
lstFiles.Items.Add(FilePath)
Next
End Sub
We use the above sub to read the contents of the hard disk folder we have
selected.
Comments
There are no comments yet. Be the first to comment!