From e3a54ea81e5e90f751a7bd4b7819744ad8a35cb5 Mon Sep 17 00:00:00 2001 From: Nick Drouin Date: Thu, 5 Dec 2019 22:14:53 -0500 Subject: [PATCH 1/2] Adds .NET Core 3.1 Console App project with examples for local .Open and .Open_Buffer_Init, as well as a .Open_Buffer_Init example for Azure Storage Blobs --- Project/dotnet-core/.gitignore | 433 ++++++++++++++++++ .../dotnet-core/ConsoleApp/ConsoleApp.csproj | 12 + .../dotnet-core/ConsoleApp/MediaInfoDLL.cs | 423 +++++++++++++++++ Project/dotnet-core/ConsoleApp/Program.cs | 221 +++++++++ 4 files changed, 1089 insertions(+) create mode 100644 Project/dotnet-core/.gitignore create mode 100644 Project/dotnet-core/ConsoleApp/ConsoleApp.csproj create mode 100644 Project/dotnet-core/ConsoleApp/MediaInfoDLL.cs create mode 100644 Project/dotnet-core/ConsoleApp/Program.cs diff --git a/Project/dotnet-core/.gitignore b/Project/dotnet-core/.gitignore new file mode 100644 index 000000000..12a254859 --- /dev/null +++ b/Project/dotnet-core/.gitignore @@ -0,0 +1,433 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +## +## Visual studio for Mac +## + + +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# JetBrains Rider +.idea/ +*.sln.iml + +## +## Visual Studio Code +## +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json diff --git a/Project/dotnet-core/ConsoleApp/ConsoleApp.csproj b/Project/dotnet-core/ConsoleApp/ConsoleApp.csproj new file mode 100644 index 000000000..033eb8a09 --- /dev/null +++ b/Project/dotnet-core/ConsoleApp/ConsoleApp.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/Project/dotnet-core/ConsoleApp/MediaInfoDLL.cs b/Project/dotnet-core/ConsoleApp/MediaInfoDLL.cs new file mode 100644 index 000000000..499de38dc --- /dev/null +++ b/Project/dotnet-core/ConsoleApp/MediaInfoDLL.cs @@ -0,0 +1,423 @@ +/* Copyright (c) MediaArea.net SARL. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license that can + * be found in the License.html file in the root of the source tree. + */ + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +// +// Microsoft Visual C# wrapper for MediaInfo Library +// See MediaInfo.h for help +// +// To make it working on Windows, you must put MediaInfo.dll +// in the executable folder +// +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +using System; +using System.Text; +using System.Runtime.InteropServices; + +#pragma warning disable 1591 // Disable XML documentation warnings + +namespace MediaInfoLib +{ + public enum StreamKind + { + General, + Video, + Audio, + Text, + Other, + Image, + Menu, + } + + public enum InfoKind + { + Name, + Text, + Measure, + Options, + NameText, + MeasureText, + Info, + HowTo + } + + public enum InfoOptions + { + ShowInInform, + Support, + ShowInSupported, + TypeOfValue + } + + public enum InfoFileOptions + { + FileOption_Nothing = 0x00, + FileOption_NoRecursive = 0x01, + FileOption_CloseAll = 0x02, + FileOption_Max = 0x04 + }; + + public enum Status + { + None = 0x00, + Accepted = 0x01, + Filled = 0x02, + Updated = 0x04, + Finalized = 0x08, + } + + public class MediaInfo + { + //Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory) + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_New(); + [DllImport("mediainfo")] + private static extern void MediaInfo_Delete(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Open(IntPtr Handle, IntPtr FileName); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Open_Buffer_Init(IntPtr Handle, Int64 File_Size, Int64 File_Offset); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Open_Buffer_Continue(IntPtr Handle, IntPtr Buffer, IntPtr Buffer_Size); + [DllImport("mediainfo")] + private static extern Int64 MediaInfo_Open_Buffer_Continue_GoTo_Get(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Open_Buffer_Finalize(IntPtr Handle); + [DllImport("mediainfo")] + private static extern void MediaInfo_Close(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Inform(IntPtr Handle, IntPtr Reserved); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_GetI(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch); + [DllImport("mediainfo", CharSet=CharSet.Unicode)] + private static extern IntPtr MediaInfo_Option(IntPtr Handle, IntPtr Option, IntPtr Value); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_State_Get(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfo_Count_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber); + + //Helpers + private static string String_From_UTF32_Ptr(IntPtr Ptr) + { + int Length = 0; + while (Marshal.ReadInt32(Ptr, Length)!=0) + Length+=4; + + byte[] Buffer = new byte[Length]; + Marshal.Copy(Ptr, Buffer, 0, Buffer.Length); + return new UTF32Encoding(!BitConverter.IsLittleEndian, false, false).GetString(Buffer); + } + + private static IntPtr UTF32_Ptr_From_String(string Str) + { + Encoding Codec = new UTF32Encoding(!BitConverter.IsLittleEndian, false, false); + int Length = Codec.GetByteCount(Str); + byte[] Buffer = new byte[Length+4]; + Codec.GetBytes(Str, 0, Str.Length, Buffer, 0); + IntPtr Ptr = Marshal.AllocHGlobal(Buffer.Length); + Marshal.Copy(Buffer, 0, Ptr, Buffer.Length); + return Ptr; + } + + //MediaInfo class + public MediaInfo() + { + try + { + Handle = MediaInfo_New(); + } + catch + { + Handle = (IntPtr)0; + } + if (Environment.OSVersion.ToString().IndexOf("Windows") == -1) + { + UnicodeIs32Bits=true; + Option("setlocale_LC_CTYPE", ""); + } + else + { + UnicodeIs32Bits=false; + } + } + ~MediaInfo() { if (Handle == (IntPtr)0) return; MediaInfo_Delete(Handle); } + public int Open(String FileName) + { + if (Handle == (IntPtr)0) + return 0; + if (UnicodeIs32Bits) + { + IntPtr FileName_Ptr = UTF32_Ptr_From_String(FileName); + int ToReturn = (int)MediaInfo_Open(Handle, FileName_Ptr); + Marshal.FreeHGlobal(FileName_Ptr); + return ToReturn; + } + else + { + IntPtr FileName_Ptr = Marshal.StringToHGlobalUni(FileName); + int ToReturn = (int)MediaInfo_Open(Handle, FileName_Ptr); + Marshal.FreeHGlobal(FileName_Ptr); + return ToReturn; + } + } + public int Open_Buffer_Init(Int64 File_Size, Int64 File_Offset) + { + if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Init(Handle, File_Size, File_Offset); + } + public int Open_Buffer_Continue(IntPtr Buffer, IntPtr Buffer_Size) + { + if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Continue(Handle, Buffer, Buffer_Size); + } + public Int64 Open_Buffer_Continue_GoTo_Get() + { + if (Handle == (IntPtr)0) return 0; return (Int64)MediaInfo_Open_Buffer_Continue_GoTo_Get(Handle); + } + public int Open_Buffer_Finalize() + { + if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Finalize(Handle); + } + public void Close() { if (Handle == (IntPtr)0) return; MediaInfo_Close(Handle); } + public String Inform() + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + return String_From_UTF32_Ptr(MediaInfo_Inform(Handle, (IntPtr)0)); + else + return Marshal.PtrToStringUni(MediaInfo_Inform(Handle, (IntPtr)0)); + } + public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo, InfoKind KindOfSearch) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + { + IntPtr Parameter_Ptr=UTF32_Ptr_From_String(Parameter); + String ToReturn=String_From_UTF32_Ptr(MediaInfo_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch)); + Marshal.FreeHGlobal(Parameter_Ptr); + return ToReturn; + } + else + { + IntPtr Parameter_Ptr=Marshal.StringToHGlobalUni(Parameter); + String ToReturn=Marshal.PtrToStringUni(MediaInfo_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch)); + Marshal.FreeHGlobal(Parameter_Ptr); + return ToReturn; + } + } + public String Get(StreamKind StreamKind, int StreamNumber, int Parameter, InfoKind KindOfInfo) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + return String_From_UTF32_Ptr(MediaInfo_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo)); + else + return Marshal.PtrToStringUni(MediaInfo_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo)); + } + public String Option(String Option, String Value) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + { + IntPtr Option_Ptr=UTF32_Ptr_From_String(Option); + IntPtr Value_Ptr=UTF32_Ptr_From_String(Value); + String ToReturn=String_From_UTF32_Ptr(MediaInfo_Option(Handle, Option_Ptr, Value_Ptr)); + Marshal.FreeHGlobal(Option_Ptr); + Marshal.FreeHGlobal(Value_Ptr); + return ToReturn; + } + else + { + IntPtr Option_Ptr=Marshal.StringToHGlobalUni(Option); + IntPtr Value_Ptr=Marshal.StringToHGlobalUni(Value); + String ToReturn=Marshal.PtrToStringUni(MediaInfo_Option(Handle, Option_Ptr, Value_Ptr)); + Marshal.FreeHGlobal(Option_Ptr); + Marshal.FreeHGlobal(Value_Ptr); + return ToReturn; + } + } + public int State_Get() { if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_State_Get(Handle); } + public int Count_Get(StreamKind StreamKind, int StreamNumber) { if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Count_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber); } + private IntPtr Handle; + private bool UnicodeIs32Bits; + + //Default values, if you know how to set default values in C#, say me + public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); } + public String Get(StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); } + public String Get(StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text); } + public String Option(String Option_) { return Option(Option_, ""); } + public int Count_Get(StreamKind StreamKind) { return Count_Get(StreamKind, -1); } + } + + public class MediaInfoList + { + //Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory) + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_New(); + [DllImport("mediainfo")] + private static extern void MediaInfoList_Delete(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_Open(IntPtr Handle, IntPtr FileName, IntPtr Options); + [DllImport("mediainfo")] + private static extern void MediaInfoList_Close(IntPtr Handle, IntPtr FilePos); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_Inform(IntPtr Handle, IntPtr FilePos, IntPtr Reserved); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_GetI(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_Option(IntPtr Handle, IntPtr Option, IntPtr Value); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_State_Get(IntPtr Handle); + [DllImport("mediainfo")] + private static extern IntPtr MediaInfoList_Count_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber); + + //Helpers + private static string String_From_UTF32_Ptr(IntPtr Ptr) + { + int Length = 0; + while (Marshal.ReadInt32(Ptr, Length)!=0) + Length+=4; + + byte[] Buffer = new byte[Length]; + Marshal.Copy(Ptr, Buffer, 0, Buffer.Length); + return new UTF32Encoding(!BitConverter.IsLittleEndian, false, false).GetString(Buffer); + } + + private static IntPtr UTF32_Ptr_From_String(string Str) + { + Encoding Codec = new UTF32Encoding(!BitConverter.IsLittleEndian, false, false); + int Length = Codec.GetByteCount(Str); + byte[] Buffer = new byte[Length+4]; + Codec.GetBytes(Str, 0, Str.Length, Buffer, 0); + IntPtr Ptr = Marshal.AllocHGlobal(Buffer.Length); + Marshal.Copy(Buffer, 0, Ptr, Buffer.Length); + return Ptr; + } + + //MediaInfo class + public MediaInfoList() + { + try + { + Handle = MediaInfoList_New(); + } + catch + { + Handle = (IntPtr)0; + } + if (Environment.OSVersion.ToString().IndexOf("Windows") == -1) + { + UnicodeIs32Bits=true; + Option("setlocale_LC_CTYPE", ""); // Load system default locale + } + else + { + UnicodeIs32Bits=false; + } + } + ~MediaInfoList() { if (Handle == (IntPtr)0) return; MediaInfoList_Delete(Handle); } + public int Open(String FileName, InfoFileOptions Options) + { + if (Handle == (IntPtr)0) + return 0; + if (UnicodeIs32Bits) + { +Console.WriteLine("Called"); + IntPtr FileName_Ptr = UTF32_Ptr_From_String(FileName); + int ToReturn = (int)MediaInfoList_Open(Handle, FileName_Ptr, (IntPtr)Options); + Marshal.FreeHGlobal(FileName_Ptr); + return ToReturn; + } + else + { + IntPtr FileName_Ptr = Marshal.StringToHGlobalUni(FileName); + int ToReturn = (int)MediaInfoList_Open(Handle, FileName_Ptr, (IntPtr)Options); + Marshal.FreeHGlobal(FileName_Ptr); + return ToReturn; + } + } + public void Close(int FilePos) { if (Handle == (IntPtr)0) return; MediaInfoList_Close(Handle, (IntPtr)FilePos); } + public String Inform(int FilePos) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + return String_From_UTF32_Ptr(MediaInfoList_Inform(Handle, (IntPtr)FilePos, (IntPtr)0)); + else + return Marshal.PtrToStringUni(MediaInfoList_Inform(Handle, (IntPtr)FilePos, (IntPtr)0)); + } + public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo, InfoKind KindOfSearch) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + { + IntPtr Parameter_Ptr=UTF32_Ptr_From_String(Parameter); + String ToReturn=String_From_UTF32_Ptr(MediaInfoList_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch)); + Marshal.FreeHGlobal(Parameter_Ptr); + return ToReturn; + } + else + { + IntPtr Parameter_Ptr=Marshal.StringToHGlobalUni(Parameter); + String ToReturn=Marshal.PtrToStringUni(MediaInfoList_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch)); + Marshal.FreeHGlobal(Parameter_Ptr); + return ToReturn; + } + } + public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, int Parameter, InfoKind KindOfInfo) { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + return String_From_UTF32_Ptr(MediaInfoList_GetI(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo)); + else + return Marshal.PtrToStringUni(MediaInfoList_GetI(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo)); + } + public String Option(String Option, String Value) + { + if (Handle == (IntPtr)0) + return "Unable to load MediaInfo library"; + if (UnicodeIs32Bits) + { + IntPtr Option_Ptr=UTF32_Ptr_From_String(Option); + IntPtr Value_Ptr=UTF32_Ptr_From_String(Value); + String ToReturn=String_From_UTF32_Ptr(MediaInfoList_Option(Handle, Option_Ptr, Value_Ptr)); + Marshal.FreeHGlobal(Option_Ptr); + Marshal.FreeHGlobal(Value_Ptr); + return ToReturn; + } + else + { + IntPtr Option_Ptr=Marshal.StringToHGlobalUni(Option); + IntPtr Value_Ptr=Marshal.StringToHGlobalUni(Value); + String ToReturn=Marshal.PtrToStringUni(MediaInfoList_Option(Handle, Option_Ptr, Value_Ptr)); + Marshal.FreeHGlobal(Option_Ptr); + Marshal.FreeHGlobal(Value_Ptr); + return ToReturn; + } + } + public int State_Get() { if (Handle == (IntPtr)0) return 0; return (int)MediaInfoList_State_Get(Handle); } + public int Count_Get(int FilePos, StreamKind StreamKind, int StreamNumber) { if (Handle == (IntPtr)0) return 0; return (int)MediaInfoList_Count_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber); } + private IntPtr Handle; + private bool UnicodeIs32Bits; + + //Default values, if you know how to set default values in C#, say me + public int Open(String FileName) { return Open(FileName, 0); } + public void Close() { Close(-1); } + public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(FilePos, StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); } + public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); } + public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text); } + public String Option(String Option_) { return Option(Option_, ""); } + public int Count_Get(int FilePos, StreamKind StreamKind) { return Count_Get(FilePos, StreamKind, -1); } + } + +} //NameSpace diff --git a/Project/dotnet-core/ConsoleApp/Program.cs b/Project/dotnet-core/ConsoleApp/Program.cs new file mode 100644 index 000000000..3a7a80c9b --- /dev/null +++ b/Project/dotnet-core/ConsoleApp/Program.cs @@ -0,0 +1,221 @@ +/* Copyright (c) MediaArea.net SARL. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license that can + * be found in the License.html file in the root of the source tree. + */ + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +// +// Microsoft Visual C# example for .NET Core console apps +// +// To make this example work, you must put MediaInfo.Dll +// in the "./bin/__ConfigurationName__/netcoreapp3.1/" folder with the other binaries +// and add MediaInfoLib/Source/MediaInfoDLL/MediaInfoDLL.cs to your project +// +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +using System; +using System.IO; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using MediaInfoLib; + +using Azure; +using Azure.Storage.Blobs; +using Azure.Storage.Blobs.Specialized; + +namespace ConsoleApp +{ + class Program + { + static async Task Main(string[] args) + { + String ToDisplay; + MediaInfo MI = new MediaInfo(); + + ToDisplay = MI.Option("Info_Version", "0.7.0.0;MediaInfoDLL_Example_CS;0.7.0.0"); + if (ToDisplay.Length == 0) + { + Console.WriteLine("MediaInfo.Dll: this version of the DLL is not compatible"); + return; + } + + if (ToDisplay == "Unable to load MediaInfo library") + { + Console.WriteLine("MediaInfo.Dll: was not found"); + return; + } + + //An example of how to use the library + ToDisplay += "\r\nOpen\r\n"; + var filePath = "D:\\TestMedia\\Blender\\BigBuckBunny-peach\\bbb_sunflower_1080p_30fps_normal.mp4"; + MI.Open(filePath); + + ToDisplay += "\r\n\r\nInform with Complete=true\r\n"; + MI.Option("Complete", "1"); + ToDisplay += MI.Inform(); + + Console.WriteLine(ToDisplay); + + Console.WriteLine(ExampleReadingWithFileStream()); + + Console.WriteLine(await ExampleReadingFromAzureBlobStorage()); + } + + + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + // + // This example demonstrates how to parse a local file using a FileStream + // MediaInfo does not need to parse the whole file. + // + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + static String ExampleReadingWithFileStream() + { + // Set the following for your content: + var filePath = "D:\\TestMedia\\Blender\\BigBuckBunny-peach\\bbb_sunflower_1080p_30fps_normal.mp4"; + FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + long contentLength = fileStream.Length; + + // Initialize MediaInfo + var MI = new MediaInfo(); + + // Prepare MediaInfo for buffered reading + MI.Open_Buffer_Init(contentLength, 0); + + // The parsing loop using a fixed memory buffer for reading and marshaling + const int localByteBufferSize = 64 * 1024; + byte[] localByteBuffer = new byte[localByteBufferSize]; + int numberOfBytesRead; + long totalNumberOfBytesRead = 0; + do + { + //Reading data somewhere, do what you want for this. + numberOfBytesRead = fileStream.Read(localByteBuffer, 0, localByteBufferSize); + totalNumberOfBytesRead += numberOfBytesRead; + + // Send the buffer to MediaInfo + System.Runtime.InteropServices.GCHandle GC = System.Runtime.InteropServices.GCHandle.Alloc(localByteBuffer, System.Runtime.InteropServices.GCHandleType.Pinned); + IntPtr AddrOf_Buffer_IntPtr = GC.AddrOfPinnedObject(); + Status Result = (Status)MI.Open_Buffer_Continue(AddrOf_Buffer_IntPtr, (IntPtr)numberOfBytesRead); + GC.Free(); + + if ((Result & Status.Finalized) == Status.Finalized) + break; + + // Test if MediaInfo requests to go elsewhere + long desiredSeekLocation = MI.Open_Buffer_Continue_GoTo_Get(); + if (desiredSeekLocation != -1) + { + Int64 Position = fileStream.Seek(desiredSeekLocation, SeekOrigin.Begin); //Position the file + contentLength = fileStream.Length; + MI.Open_Buffer_Init(contentLength, Position); //Informing MediaInfo we have seek + } + } + while (numberOfBytesRead > 0); + + Console.WriteLine($"Total Bytes read: {totalNumberOfBytesRead}/{contentLength} = {totalNumberOfBytesRead/ (float)contentLength}"); + + // This is the end of the stream, MediaInfo must finish some work + MI.Open_Buffer_Finalize(); + + // Use MediaInfoLib as needed + MI.Option("Complete", "1"); + string ToDisplay = MI.Inform(); + + return ToDisplay; + } + + + + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + // + // This example demonstrates how to parse a file in Azure Blob Storage using a connection string for auth. + // MediaInfo does not need to parse the whole remote file. + // + // Uses BlobBaseClient.DownloadAsync for byte-range downloads. + // Ref: https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadasync + // Ref: https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs#L243 + // + // Add the following nuget PackageReference your project file: + // Azure.Storage.Blobs (https://www.nuget.org/packages/Azure.Storage.Blobs/) + // + // Add to your implementation class: + // using Azure.Storage.Blobs.Specialized; + // + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + static async Task ExampleReadingFromAzureBlobStorage() + { + // Set the following for your content: + var fileUri = new Uri("https://mystorageaccount.blob.core.windows.net/blendercontainer/bbb_sunflower_1080p_30fps_normal.mp4"); + var connectionString = "DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=..."; + + var blobUriBuilder = new BlobUriBuilder(fileUri); // The blobUriBuild simplifies the parsing of Azure Storage Uris + BlobBaseClient blobBaseClient = new BlobBaseClient(connectionString, blobUriBuilder.BlobContainerName, blobUriBuilder.BlobName); + + // Initialize MediaInfo + var MI = new MediaInfo(); + + // Do a HEAD on the file: + var blobProperties = await blobBaseClient.GetPropertiesAsync(); + long contentLength = blobProperties.Value.ContentLength; + + // Prepare MediaInfo for buffered reading + MI.Open_Buffer_Init(contentLength, 0); + + // The parsing loop using a fixed memory buffer for reading and marshaling + const int localByteBufferSize = 64 * 1024; + byte[] localByteBuffer = new byte[localByteBufferSize]; + int numberOfBytesRead; + long currentPosition = 0; + long desiredOffset = 0; + long totalNumberOfBytesRead = 0; + do + { + // Read remote byte-range into localByteBuffer + var downloadResponse = await blobBaseClient.DownloadAsync(new HttpRange(desiredOffset, localByteBufferSize)); + numberOfBytesRead = (int)downloadResponse.Value.ContentLength; + using (var ms = new MemoryStream(localByteBufferSize)) + { + downloadResponse.Value.Content.CopyTo(ms); + localByteBuffer = ms.GetBuffer(); + currentPosition = downloadResponse.Value.Content.Position; + } + totalNumberOfBytesRead += numberOfBytesRead; + + // Send the buffer to MediaInfo + GCHandle GC = GCHandle.Alloc(localByteBuffer, GCHandleType.Pinned); + IntPtr AddrOf_Buffer_IntPtr = GC.AddrOfPinnedObject(); + Status Result = (Status)MI.Open_Buffer_Continue(AddrOf_Buffer_IntPtr, (IntPtr)numberOfBytesRead); + GC.Free(); + + if ((Result & Status.Finalized) == Status.Finalized) + break; + + // Test if MediaInfo requests to go elsewhere + desiredOffset = MI.Open_Buffer_Continue_GoTo_Get(); + if (desiredOffset != -1) + { + // Inform MediaInfo we have seek + MI.Open_Buffer_Init(contentLength, desiredOffset); + } + else + { + // Adjust the byte-range request offset + desiredOffset = currentPosition; + } + } + while (numberOfBytesRead > 0 && desiredOffset != contentLength); + + Console.WriteLine($"Total Bytes read: {totalNumberOfBytesRead}/{contentLength} = {totalNumberOfBytesRead / (float)contentLength}"); + + // This is the end of the stream, MediaInfo must finish some work + MI.Open_Buffer_Finalize(); + + // Use MediaInfoLib as needed + MI.Option("Complete", "1"); + string ToDisplay = MI.Inform(); + + return ToDisplay; + } + } +} From cb0fb672e7217d706037310fc4382a12cfbb0e12 Mon Sep 17 00:00:00 2001 From: Nick Drouin Date: Fri, 6 Dec 2019 09:50:19 -0500 Subject: [PATCH 2/2] Update Program.cs Removed MemoryStream. Increased localByteBufferSize to 1MB after doing performance testing against azure for both single and mult-block blobs, with moov boxes at the begining and end of an mp4 file. --- Project/dotnet-core/ConsoleApp/Program.cs | 28 ++++++++++------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Project/dotnet-core/ConsoleApp/Program.cs b/Project/dotnet-core/ConsoleApp/Program.cs index 3a7a80c9b..c43ac2087 100644 --- a/Project/dotnet-core/ConsoleApp/Program.cs +++ b/Project/dotnet-core/ConsoleApp/Program.cs @@ -101,7 +101,7 @@ static String ExampleReadingWithFileStream() if ((Result & Status.Finalized) == Status.Finalized) break; - + // Test if MediaInfo requests to go elsewhere long desiredSeekLocation = MI.Open_Buffer_Continue_GoTo_Get(); if (desiredSeekLocation != -1) @@ -113,7 +113,7 @@ static String ExampleReadingWithFileStream() } while (numberOfBytesRead > 0); - Console.WriteLine($"Total Bytes read: {totalNumberOfBytesRead}/{contentLength} = {totalNumberOfBytesRead/ (float)contentLength}"); + Console.WriteLine($"Total Bytes read: {totalNumberOfBytesRead}/{contentLength} = {totalNumberOfBytesRead / (float)contentLength}"); // This is the end of the stream, MediaInfo must finish some work MI.Open_Buffer_Finalize(); @@ -126,7 +126,7 @@ static String ExampleReadingWithFileStream() } - + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // // This example demonstrates how to parse a file in Azure Blob Storage using a connection string for auth. @@ -149,12 +149,12 @@ static async Task ExampleReadingFromAzureBlobStorage() var fileUri = new Uri("https://mystorageaccount.blob.core.windows.net/blendercontainer/bbb_sunflower_1080p_30fps_normal.mp4"); var connectionString = "DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=..."; - var blobUriBuilder = new BlobUriBuilder(fileUri); // The blobUriBuild simplifies the parsing of Azure Storage Uris + var blobUriBuilder = new BlobUriBuilder(fileUri); // The blobUriBuilder simplifies the parsing of Azure Storage Uris BlobBaseClient blobBaseClient = new BlobBaseClient(connectionString, blobUriBuilder.BlobContainerName, blobUriBuilder.BlobName); // Initialize MediaInfo var MI = new MediaInfo(); - + // Do a HEAD on the file: var blobProperties = await blobBaseClient.GetPropertiesAsync(); long contentLength = blobProperties.Value.ContentLength; @@ -163,10 +163,10 @@ static async Task ExampleReadingFromAzureBlobStorage() MI.Open_Buffer_Init(contentLength, 0); // The parsing loop using a fixed memory buffer for reading and marshaling - const int localByteBufferSize = 64 * 1024; + const int localByteBufferSize = 1 * 1024 * 1024; byte[] localByteBuffer = new byte[localByteBufferSize]; int numberOfBytesRead; - long currentPosition = 0; + long currentPosition; long desiredOffset = 0; long totalNumberOfBytesRead = 0; do @@ -174,12 +174,8 @@ static async Task ExampleReadingFromAzureBlobStorage() // Read remote byte-range into localByteBuffer var downloadResponse = await blobBaseClient.DownloadAsync(new HttpRange(desiredOffset, localByteBufferSize)); numberOfBytesRead = (int)downloadResponse.Value.ContentLength; - using (var ms = new MemoryStream(localByteBufferSize)) - { - downloadResponse.Value.Content.CopyTo(ms); - localByteBuffer = ms.GetBuffer(); - currentPosition = downloadResponse.Value.Content.Position; - } + downloadResponse.Value.Content.Read(localByteBuffer, 0, numberOfBytesRead); + currentPosition = desiredOffset + numberOfBytesRead; totalNumberOfBytesRead += numberOfBytesRead; // Send the buffer to MediaInfo @@ -196,7 +192,7 @@ static async Task ExampleReadingFromAzureBlobStorage() if (desiredOffset != -1) { // Inform MediaInfo we have seek - MI.Open_Buffer_Init(contentLength, desiredOffset); + MI.Open_Buffer_Init(contentLength, desiredOffset); } else { @@ -204,12 +200,12 @@ static async Task ExampleReadingFromAzureBlobStorage() desiredOffset = currentPosition; } } - while (numberOfBytesRead > 0 && desiredOffset != contentLength); + while (numberOfBytesRead > 0 && desiredOffset != contentLength); Console.WriteLine($"Total Bytes read: {totalNumberOfBytesRead}/{contentLength} = {totalNumberOfBytesRead / (float)contentLength}"); // This is the end of the stream, MediaInfo must finish some work - MI.Open_Buffer_Finalize(); + MI.Open_Buffer_Finalize(); // Use MediaInfoLib as needed MI.Option("Complete", "1");