Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

.TS File Convertor to .MP4

3.00/5 (2 votes)
4 May 2021CPOL 4.9K  
How to convert .ts files to .mp4
This is the program which I created using VB.NET. A user can use it for converting .ts files to .mp4

Introduction

.ts files are mostly used in live streaming programs. We can use many video converting programs like Sony Vega and Wondershare to convert the .ts files, but for this program, I only want to convert .ts file to .mp4 using VB.NET and with cmd.exe and FFMEG.exe.

Image 1

You can download the source code from the URL below:

Using the Code

VB.NET
Imports System.Threading
Imports System.Diagnostics
Imports System.IO
Public Class FormConvert
    Dim ASecond As Integer = 0
    Dim AMins As Integer = 0
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnCovert.Enabled = False
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCovert.Click

        btnCovert.Enabled = False
        Dim FileName1 As String = System.IO.Path.GetFileNameWithoutExtension_
            (OpenFileDialog1.FileName) 'Get only Flie name without extension
        Dim FileName2 As String = OpenFileDialog1.FileName
        Dim programName As String = "ffmpeg.exe"
        System.Diagnostics.Process.Start(Application.StartupPath & "\" & programName)

        '============================================================================

        Dim active As Boolean = True
        Dim ts As TimeSpan
        Dim process As Process
        Dim sw As New Stopwatch
        AMins = 0
        ASecond = 0
        sw.Start()
        process = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & _
                  " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
        process.WaitForExit()
        sw.Stop()
        If process.HasExited Then
            OutFileInfor()
        End If
        ts = sw.Elapsed
        ' Format and display the TimeSpan value.
        Dim elapsedTime As String = String.Format("Spend Time: {0:00}:{1:00}:{2:00}.{3:00}", _
                        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
        lbTime.Text = elapsedTime

        '============================================================================
        'btnStopTime.PerformClick()
        'Dim p As System.Diagnostics.Process
        'Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & _
                       "Down\" & FileName1 & ".mp4")
        'p = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & _
                           "Down\" & FileName1 & ".mp4")
        'p = System.Diagnostics.Process.Start("cmd.exe", "/c ffmpeg.exe -i " & _
             FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        Dim R1 As DialogResult = OpenFileDialog1.ShowDialog()
        Dim path1 As String = OpenFileDialog1.FileName 'Get part file
        Dim FileName1 As String = System.IO.Path.GetFileNameWithoutExtension_
                      (OpenFileDialog1.FileName) 'Get only Flie name without extension
        Dim MyFileSize = My.Computer.FileSystem.GetFileInfo(path1)

        If R1 = Windows.Forms.DialogResult.OK Then
            txtOnlyPathFile.Text = path1
            btnCovert.Enabled = True
            txtOnlyFileName.Text = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        ElseIf R1 = Windows.Forms.DialogResult.Cancel Then
            txtOnlyFileName.Text = ""
            txtOnlyPathFile.Text = ""
            On Error GoTo nextstep
        End If
nextstep:
        LbInformation.Text = "File    Infor: " & System.IO.Path.GetFileName_
                              (OpenFileDialog1.FileName) & " S: " & _
                              GetFileSize(path1) & ", L: " & GetMyDuration(path1)
    End Sub

    Public Function GetFileSize(ByVal TheFile As String) As String
        Dim DoubleBytes As Double
        If TheFile.Length = 0 Then Return ""
        If Not System.IO.File.Exists(TheFile) Then Return ""
        Dim TheSize As ULong = My.Computer.FileSystem.GetFileInfo(TheFile).Length
        Dim SizeType As String = ""
        Try
            Select Case TheSize
                Case Is >= 1099511627776
                    DoubleBytes = CDbl(TheSize / 1099511627776) 'TB
                    Return FormatNumber(DoubleBytes, 2) & " TB"
                Case 1073741824 To 1099511627775
                    DoubleBytes = CDbl(TheSize / 1073741824) 'GB
                    Return FormatNumber(DoubleBytes, 2) & " GB"
                Case 1048576 To 1073741823
                    DoubleBytes = CDbl(TheSize / 1048576) 'MB
                    Return FormatNumber(DoubleBytes, 2) & " MB"
                Case 1024 To 1048575
                    DoubleBytes = CDbl(TheSize / 1024) 'KB
                    Return FormatNumber(DoubleBytes, 2) & " KB"
                Case 0 To 1023
                    DoubleBytes = TheSize ' bytes
                    Return FormatNumber(DoubleBytes, 2) & " bytes"
                Case Else
                    Return ""
            End Select
        Catch
            Return ""
        End Try
    End Function

    Private Sub bFolder_Click(sender As Object, e As EventArgs) Handles bFolder.Click
        Process.Start("explorer.exe", _
                       Application.StartupPath) 'Application.StartupPath Property
    End Sub

    Function GetMyDuration(ByVal MovieFullPath As String) As String
        If File.Exists(MovieFullPath) Then
            Dim objShell As Object = CreateObject("Shell.Application")
            Dim objFolder As Object =
         objShell.Namespace(Path.GetDirectoryName(MovieFullPath))
            For Each strFileName In objFolder.Items
                If strFileName.Name = Path.GetFileName(MovieFullPath) Then
                    Return objFolder.GetDetailsOf(strFileName, 27).ToString
                    Exit For
                    Exit Function
                End If
            Next
            Return ""
        Else
            Return ""
        End If
    End Function

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ASecond += 1
        With TimeSpan.FromSeconds(ASecond)
            If ASecond = 60 Then
                AMins += 1
                ASecond = 0
            End If
        End With
        lbTime.Text = "Run Time: " & AMins & " mins : " & ASecond & " s"
    End Sub

    Private Sub OutFileInfor()
        Dim OutFileName As String = System.IO.Path.GetFileNameWithoutExtension_
                                    (OpenFileDialog1.FileName)
        Dim OutFilePart As String = Application.StartupPath
        OutFilePart = OutFilePart & "\Down\" & OutFileName & ".mp4"
        LbOut.Text = "File Output: " & OutFileName & ".mp4, S: " & _
                      GetFileSize(OutFilePart) & ", L: " & GetMyDuration(OutFilePart)
    End Sub

End Class

Points of Interest

I really hope this program will help you to have more ideas to create your own program and upgrade it to share for free.

History

I spent nearly two weeks creating this program because I stopped programming nearly ten years ago. But I created it because I needed a free video convertor that converts .ts files to .mp4. That is why I tried to build this program and I was lucky enough to have two programmers here help me.

I really want to take this opportunity to thank Richard MacCutchan and Dave Kreskowiak who always shared knowledge with me while I was coding this program.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)