#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

Namespace CSFFmpegExample
    Public Partial Class frmMediaProperties
        Inherits Form

        Private WithEvents propertyGrid1 As PropertyGrid

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

        Public Async Sub LoadMediaFile(ByVal MediaFile As String)
            'Create a new instance of the library with the direction of the ffmpeg libraries
            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]\

            Dim cSFFmpeg = New CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath)
            Dim mediaProperties As CSMediaProperties.MediaProperties = Await cSFFmpeg.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 Sub

        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(703, 410)
            Me.propertyGrid1.TabIndex = 87
            '
            'frmMediaProperties
            '
            Me.ClientSize = New System.Drawing.Size(703, 410)
            Me.Controls.Add(Me.propertyGrid1)
            Me.Name = "frmMediaProperties"
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.Text = "frmMediaProperties"
            Me.ResumeLayout(False)

        End Sub
    End Class
End Namespace
