@@ -20,6 +20,21 @@ public static class FFmpegManager
2020 /// </summary>
2121 public static bool IsFFmpegSetup { get ; private set ; } = false ;
2222
23+ /// <summary>
24+ /// Set that to true if you're struggling to setup FFmpeg properly.
25+ /// </summary>
26+ public static bool LogLibrarySearch { get ; set ; } = false ;
27+
28+ private static void llsLog ( string message )
29+ {
30+ if ( LogLibrarySearch )
31+ {
32+ Console . ForegroundColor = ConsoleColor . DarkCyan ;
33+ Console . Error . WriteLine ( message ) ;
34+ Console . ResetColor ( ) ;
35+ }
36+ }
37+
2338 /// <summary>
2439 /// Informative version string. It is usually the actual release version number or a git commit description. It has no fixed format and can change any time. It should never be parsed by code.
2540 /// <br/>
@@ -64,6 +79,12 @@ public static void SetupFFmpeg(FFmpegLogLevel logLevel, ConsoleColor logColor, p
6479 if ( IsFFmpegSetup )
6580 return ;
6681
82+ llsLog ( "Setting up FFmpeg\n Required libraries:" +
83+ $ "\n - avcodec (v{ ffmpeg . LIBAVCODEC_VERSION_MAJOR } )" +
84+ $ "\n - avdevice (v{ ffmpeg . LIBAVDEVICE_VERSION_MAJOR } )" +
85+ $ "\n - avformat (v{ ffmpeg . LIBAVFORMAT_VERSION_MAJOR } )" +
86+ $ "\n - swscale (v{ ffmpeg . LIBSWSCALE_VERSION_MAJOR } )") ;
87+
6788 var requiredLibs = LF . AVCodec | LF . AVDevice | LF . AVFormat | LF . SWScale ;
6889
6990 if ( paths . Length == 0 )
@@ -104,6 +125,40 @@ internal static unsafe void SetupFFmpegLogging(FFmpegLogLevel logLevel, ConsoleC
104125 ffmpeg . av_log_set_callback ( logCallback ) ;
105126 }
106127
128+ /// <summary>
129+ /// Tries to set the RootPath to the first path in which it can find all the native libraries.
130+ /// Ideally, you would want to only call this function once, before doing anything with FFmpeg.
131+ /// </summary>
132+ /// <remarks>
133+ /// This function will not load the native libraries but merely check if they exist.
134+ /// </remarks>
135+ /// <param name="paths">Every path to try out. It will set the RootPath to the first one that works.</param>
136+ public static void TrySetRootPath ( params string [ ] paths ) => TrySetRootPath ( paths ) ;
137+
138+ /// <summary>
139+ /// Tries to set the RootPath to the first path in which it can find all the required native libraries.
140+ /// Ideally, you would want to only call this function once, before doing anything with FFmpeg.
141+ /// </summary>
142+ /// <remarks>
143+ /// This function will not load the native libraries but merely check if they exist.
144+ /// </remarks>
145+ /// <param name="requiredLibraries">The required libraries. If you don't need all of them, you can specify them here.</param>
146+ /// <param name="paths">Every path to try out. It will set the RootPath to the first one that works.</param>
147+ public static void TrySetRootPath ( LF requiredLibraries , params string [ ] paths )
148+ {
149+ try
150+ {
151+ ffmpeg . RootPath = paths . First ( ( path ) => CanLoadLibraries ( requiredLibraries , path ) ) ;
152+ }
153+ catch ( InvalidOperationException )
154+ {
155+ string pathList = "\n - " + string . Join ( "\n - " , paths ) ;
156+ throw new InvalidOperationException (
157+ $ "Couldn't find native libraries in the following paths:{ pathList } " +
158+ "\n Make sure you installed the correct versions of the native libraries." ) ;
159+ }
160+ }
161+
107162 /// <summary>
108163 /// Tries to load the native libraries from the set root path. <br/>
109164 /// You can specify which libraries need to be loaded with LibraryFlags.
@@ -112,9 +167,10 @@ internal static unsafe void SetupFFmpegLogging(FFmpegLogLevel logLevel, ConsoleC
112167 /// If you try to do that later, it might unload all of your already loaded libraries and fail to provide them again.
113168 /// </summary>
114169 /// <returns>Whether it succeeded in loading all the requested libraries.</returns>
115- public static bool CanLoadLibraries ( LibraryFlags libraries = LibraryFlags . All , string path = "" )
170+ public static bool CanLoadLibraries ( LF libraries = LF . All , string path = "" )
116171 {
117172 var validated = new List < string > ( ) ;
173+ llsLog ( $ "Searching for libraries in { path } ") ;
118174 return libraries . ToStrings ( ) . All ( ( lib ) => canLoadLibrary ( lib , path , validated ) ) ;
119175 }
120176
@@ -128,46 +184,14 @@ private static bool canLoadLibrary(string lib, string path, List<string> validat
128184 if ( validated . Contains ( lib ) )
129185 return true ;
130186
131- var version = ffmpeg . LibraryVersionMap [ lib ] ;
187+ int version = ffmpeg . LibraryVersionMap [ lib ] ;
132188 if ( ! canLoadNativeLibrary ( path , lib , version ) )
133189 return false ;
134190
135191 validated . Add ( lib ) ;
136192 return true ;
137193 }
138194
139- /// <summary>
140- /// Tries to set the RootPath to the first path in which it can find all the native libraries.
141- /// Ideally, you would want to only call this function once, before doing anything with FFmpeg.
142- /// </summary>
143- /// <remarks>
144- /// This function will not load the native libraries but merely check if they exist.
145- /// </remarks>
146- /// <param name="paths">Every path to try out. It will set the RootPath to the first one that works.</param>
147- public static void TrySetRootPath ( params string [ ] paths ) => TrySetRootPath ( paths ) ;
148-
149- /// <summary>
150- /// Tries to set the RootPath to the first path in which it can find all the required native libraries.
151- /// Ideally, you would want to only call this function once, before doing anything with FFmpeg.
152- /// </summary>
153- /// <remarks>
154- /// This function will not load the native libraries but merely check if they exist.
155- /// </remarks>
156- /// <param name="requiredLibraries">The required libraries. If you don't need all of them, you can specify them here.</param>
157- /// <param name="paths">Every path to try out. It will set the RootPath to the first one that works.</param>
158- public static void TrySetRootPath ( LibraryFlags requiredLibraries , params string [ ] paths )
159- {
160- try
161- {
162- ffmpeg . RootPath = paths . First ( ( path ) => CanLoadLibraries ( requiredLibraries , path ) ) ;
163- }
164- catch ( InvalidOperationException )
165- {
166- var pathList = paths . Aggregate ( ( a , b ) => $ "'{ a } ', '{ b } '") ;
167- throw new InvalidOperationException ( $ "Couldn't find native libraries in the following paths: { pathList } ") ;
168- }
169- }
170-
171195 /// <summary>
172196 /// Checks if it can load a native library using platform naming conventions.
173197 /// </summary>
@@ -177,9 +201,11 @@ public static void TrySetRootPath(LibraryFlags requiredLibraries, params string[
177201 /// <returns>Whether it found the native library in the path.</returns>
178202 private static bool canLoadNativeLibrary ( string path , string libraryName , int version )
179203 {
180- var nativeLibraryName = LibraryLoader . GetNativeLibraryName ( libraryName , version ) ;
181- var fullName = Path . Combine ( path , nativeLibraryName ) ;
182- return File . Exists ( fullName ) ;
204+ string nativeLibraryName = LibraryLoader . GetNativeLibraryName ( libraryName , version ) ;
205+ string fullName = Path . Combine ( path , nativeLibraryName ) ;
206+ bool exists = File . Exists ( fullName ) ;
207+ llsLog ( $ " { ( exists ? "Found" : "Couldn't find" ) } library { nativeLibraryName } ") ;
208+ return exists ;
183209 }
184210 }
185211}
0 commit comments