#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. 
//
#endregion

namespace CSFFmpegExample
{
    using System.Diagnostics;
    using System.Linq;
    using System.Windows.Forms;

    public partial class frmMediaProperties : Form
    {
        public frmMediaProperties()
        {
            InitializeComponent();
        }

        public async void LoadMediaFile(string MediaFile)
        {
            //Create a new instance of the library with the direction of the ffmpeg libraries
            string LibsPath = "";

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

            var cSFFmpeg = new CSFFmpeg.CSFFmpeg("UserName", "RegKey", LibsPath);

            CSMediaProperties.MediaProperties mediaProperties = await cSFFmpeg.Open(MediaFile);

            //Check if the ffprobe can open the media file
            if (mediaProperties.Format == null)
            {
                MessageBox.Show("Cannot open media file");
                return;
            }

            //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:
            foreach (var stream in mediaProperties.Streams.Stream)
            {
                Debug.WriteLine("stream.Duration index " + stream.Index);
                Debug.WriteLine(stream.Duration);
                //etc..
            }

            //You can access to the id3 tags like this:

            //Check if the media file contains id3 title tag:
            if (mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "title") != null)
            {
                //The id3 title tag existed, print it:
                Debug.WriteLine("ID3 tag title");
                Debug.WriteLine(mediaProperties.Format.Tags.FirstOrDefault(tag => tag.Key == "title").Value);
            }

            //Set the propertyGrid1
            propertyGrid1.SelectedObject = mediaProperties;

            //Expand all the items
            propertyGrid1.ExpandAllGridItems();
        }
    }
}
