#Region "LICENSE"
'
' Copyright (c) Microncode.com
' 
' This source is subject to the Microsoft Public License. 
' See https://opensource.org/licenses/ms-pl.html.
' All other rights reserved. 
' 
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
'
#End Region

Imports System.Diagnostics
Imports System.Linq
Imports System.Windows.Forms
Imports CSMediaProperties

Partial Public Class frmMediaProperties
    Inherits Form

    Private WithEvents propertyGrid1 As PropertyGrid
    Private _CSVideoConverter As CSVideoConverter.VideoConverter

    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Public Async Sub LoadMediaFile(ByVal MediaFile As String)
        'Open the source media file and get the details to the mediaProperties object

        Dim LibsPath As String = ""


        'If the LibsPath is empty or "" the library will search for
        'the ffmpeg libraries under the path:
        '[AssemblyDirectory]\CSPlugins\FFmpeg\[x86|x64]\

        Using New CSVideoConverter.VideoConverter("UserName", "RegKey", LibsPath)
            Dim mediaProperties As MediaProperties
            mediaProperties = Await _CSVideoConverter.Open(MediaFile)


            'Check if the ffprobe can open the media file
            If mediaProperties.Format Is Nothing Then
                MessageBox.Show("Cannot open media file")
                Return
            End If


            'Now the mediaProperties contains all the information about the media file.
            'mediaProperties.Format.BitRate
            'mediaProperties.Format.Duration
            ' etc..

            'You can access all the information for each stream in the streams like this:
            For Each stream In mediaProperties.Streams.Stream
                Debug.WriteLine("stream.Duration index " & stream.Index)
                Debug.WriteLine(stream.Duration)
                'etc..
            Next


            'You can access to the id3 tags like this:

            'Check if the media file contains id3 title tag:
            If mediaProperties.Format.Tags.FirstOrDefault(Function(tag) Equals(tag.Key, "title")) IsNot Nothing Then
                'The id3 title tag existed, print it:
                Debug.WriteLine("ID3 tag title")
                Debug.WriteLine(Enumerable.FirstOrDefault(mediaProperties.Format.Tags, Function(tag) Equals(tag.Key, "title")).Value)
            End If


            'Set the propertyGrid1
            Me.propertyGrid1.SelectedObject = mediaProperties

            'Expand all the items
            Me.propertyGrid1.ExpandAllGridItems()
        End Using
    End Sub

    Private Class CSharpImpl
        <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    End Class

    Private Sub InitializeComponent()
        Me.propertyGrid1 = New System.Windows.Forms.PropertyGrid()
        Me.SuspendLayout()
        '
        'propertyGrid1
        '
        Me.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.propertyGrid1.Location = New System.Drawing.Point(0, 0)
        Me.propertyGrid1.Name = "propertyGrid1"
        Me.propertyGrid1.Size = New System.Drawing.Size(800, 450)
        Me.propertyGrid1.TabIndex = 87
        '
        'frmMediaProperties
        '
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.propertyGrid1)
        Me.Name = "frmMediaProperties"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Media File Properties"
        Me.ResumeLayout(False)

    End Sub
End Class

