* work around Mac accent encoding issues

This commit is contained in:
Reinhard Pointner 2015-05-24 22:53:47 +00:00
parent ab086af06f
commit 457c74129b
3 changed files with 94 additions and 42 deletions

BIN
lib/native/mac-x86_64/libmediainfo.dylib Normal file → Executable file

Binary file not shown.

View File

@ -3,6 +3,7 @@ package net.filebot.mediainfo;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.Normalizer; import java.text.Normalizer;
import java.text.Normalizer.Form; import java.text.Normalizer.Form;
import java.util.ArrayList; import java.util.ArrayList;
@ -10,6 +11,7 @@ import java.util.EnumMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger;
import com.sun.jna.NativeLibrary; import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform; import com.sun.jna.Platform;
@ -42,17 +44,50 @@ public class MediaInfo implements Closeable {
} }
public synchronized boolean open(File file) { public synchronized boolean open(File file) {
if (!file.isFile()) if (!file.isFile()) {
return false; return false;
// MacOS filesystem may require NFD unicode decomposition (forcing NFD seems to work for System.out() but passing to libmediainfo is still not working)
String path = file.getAbsolutePath();
if (Platform.isMac()) {
path = Normalizer.normalize(path, Form.NFD);
} }
String path = file.getAbsolutePath();
// on Mac files that contain accents cannot be opened via JNA WString file paths due to encoding differences so we use the buffer interface instead for these files
if (Platform.isMac() && path.length() != Normalizer.normalize(path, Form.NFD).length()) {
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
return openViaBuffer(raf);
} catch (IOException e) {
return false;
}
}
return MediaInfoLibrary.INSTANCE.Open(handle, new WString(path)) > 0; return MediaInfoLibrary.INSTANCE.Open(handle, new WString(path)) > 0;
} }
private boolean openViaBuffer(RandomAccessFile f) throws IOException {
byte[] buffer = new byte[64 * 1024];
int read = -1;
if (MediaInfoLibrary.INSTANCE.Open_Buffer_Init(handle, f.length(), 0) <= 0) {
return false;
}
do {
read = f.read(buffer);
int result = MediaInfoLibrary.INSTANCE.Open_Buffer_Continue(handle, buffer, read);
if ((result & 8) == 8) {
break;
}
if (MediaInfoLibrary.INSTANCE.Open_Buffer_Continue_GoTo_Get(handle) != -1) {
long gotoPos = MediaInfoLibrary.INSTANCE.Open_Buffer_Continue_GoTo_Get(handle);
f.seek(gotoPos);
MediaInfoLibrary.INSTANCE.Open_Buffer_Init(handle, f.length(), gotoPos);
}
} while (read > 0);
MediaInfoLibrary.INSTANCE.Open_Buffer_Finalize(handle);
return true;
}
public synchronized String inform() { public synchronized String inform() {
return MediaInfoLibrary.INSTANCE.Inform(handle).toString(); return MediaInfoLibrary.INSTANCE.Inform(handle).toString();
} }

View File

@ -1,7 +1,5 @@
package net.filebot.mediainfo; package net.filebot.mediainfo;
import static java.util.Collections.*; import static java.util.Collections.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -13,7 +11,6 @@ import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer; import com.sun.jna.Pointer;
import com.sun.jna.WString; import com.sun.jna.WString;
interface MediaInfoLibrary extends Library { interface MediaInfoLibrary extends Library {
MediaInfoLibrary INSTANCE = (MediaInfoLibrary) Native.loadLibrary("mediainfo", MediaInfoLibrary.class, singletonMap(OPTION_FUNCTION_MAPPER, new FunctionMapper() { MediaInfoLibrary INSTANCE = (MediaInfoLibrary) Native.loadLibrary("mediainfo", MediaInfoLibrary.class, singletonMap(OPTION_FUNCTION_MAPPER, new FunctionMapper() {
@ -25,7 +22,6 @@ interface MediaInfoLibrary extends Library {
} }
})); }));
/** /**
* Create a new handle. * Create a new handle.
* *
@ -33,28 +29,28 @@ interface MediaInfoLibrary extends Library {
*/ */
Pointer New(); Pointer New();
/** /**
* Open a file and collect information about it (technical information and tags). * Open a file and collect information about it (technical information and tags).
* *
* @param handle * @param handle
* @param file full name of the file to open * @param file
* full name of the file to open
* @return 1 if file was opened, 0 if file was not not opened * @return 1 if file was opened, 0 if file was not not opened
*/ */
int Open(Pointer handle, WString file); int Open(Pointer handle, WString file);
/** /**
* Configure or get information about MediaInfo. * Configure or get information about MediaInfo.
* *
* @param handle * @param handle
* @param option The name of option * @param option
* @param value The value of option * The name of option
* @param value
* The value of option
* @return Depends on the option: by default "" (nothing) means No, other means Yes * @return Depends on the option: by default "" (nothing) means No, other means Yes
*/ */
WString Option(Pointer handle, WString option, WString value); WString Option(Pointer handle, WString option, WString value);
/** /**
* Get all details about a file. * Get all details about a file.
* *
@ -63,50 +59,52 @@ interface MediaInfoLibrary extends Library {
*/ */
WString Inform(Pointer handle); WString Inform(Pointer handle);
/** /**
* Get a piece of information about a file (parameter is a string). * Get a piece of information about a file (parameter is a string).
* *
* @param handle * @param handle
* @param streamKind Kind of stream (general, video, audio...) * @param streamKind
* @param streamNumber Stream number in Kind of stream (first, second...) * Kind of stream (general, video, audio...)
* @param parameter Parameter you are looking for in the stream (Codec, width, bitrate...), * @param streamNumber
* in string format ("Codec", "Width"...) * Stream number in Kind of stream (first, second...)
* @param infoKind Kind of information you want about the parameter (the text, the measure, * @param parameter
* the help...) * Parameter you are looking for in the stream (Codec, width, bitrate...), in string format ("Codec", "Width"...)
* @param searchKind Where to look for the parameter * @param infoKind
* Kind of information you want about the parameter (the text, the measure, the help...)
* @param searchKind
* Where to look for the parameter
* @return a string about information you search, an empty string if there is a problem * @return a string about information you search, an empty string if there is a problem
*/ */
WString Get(Pointer handle, int streamKind, int streamNumber, WString parameter, int infoKind, int searchKind); WString Get(Pointer handle, int streamKind, int streamNumber, WString parameter, int infoKind, int searchKind);
/** /**
* Get a piece of information about a file (parameter is an integer). * Get a piece of information about a file (parameter is an integer).
* *
* @param handle * @param handle
* @param streamKind Kind of stream (general, video, audio...) * @param streamKind
* @param streamNumber Stream number in Kind of stream (first, second...) * Kind of stream (general, video, audio...)
* @param parameter Parameter you are looking for in the stream (Codec, width, bitrate...), * @param streamNumber
* in integer format (first parameter, second parameter...) * Stream number in Kind of stream (first, second...)
* @param infoKind Kind of information you want about the parameter (the text, the measure, * @param parameter
* the help...) * Parameter you are looking for in the stream (Codec, width, bitrate...), in integer format (first parameter, second parameter...)
* @param infoKind
* Kind of information you want about the parameter (the text, the measure, the help...)
* @return a string about information you search, an empty string if there is a problem * @return a string about information you search, an empty string if there is a problem
*/ */
WString GetI(Pointer handle, int streamKind, int streamNumber, int parameterIndex, int infoKind); WString GetI(Pointer handle, int streamKind, int streamNumber, int parameterIndex, int infoKind);
/** /**
* Count of streams of a stream kind (StreamNumber not filled), or count of piece of * Count of streams of a stream kind (StreamNumber not filled), or count of piece of information in this stream.
* information in this stream.
* *
* @param handle * @param handle
* @param streamKind Kind of stream (general, video, audio...) * @param streamKind
* @param streamNumber Stream number in this kind of stream (first, second...) * Kind of stream (general, video, audio...)
* @param streamNumber
* Stream number in this kind of stream (first, second...)
* @return number of streams of the given stream kind * @return number of streams of the given stream kind
*/ */
int Count_Get(Pointer handle, int streamKind, int streamNumber); int Count_Get(Pointer handle, int streamKind, int streamNumber);
/** /**
* Close a file opened before with Open(). * Close a file opened before with Open().
* *
@ -114,7 +112,6 @@ interface MediaInfoLibrary extends Library {
*/ */
void Close(Pointer handle); void Close(Pointer handle);
/** /**
* Dispose of a handle created with New(). * Dispose of a handle created with New().
* *
@ -122,4 +119,24 @@ interface MediaInfoLibrary extends Library {
*/ */
void Delete(Pointer handle); void Delete(Pointer handle);
/**
* Open_Buffer_Init
*/
int Open_Buffer_Init(Pointer handle, long length, long offset);
/**
* Open_Buffer_Continue
*/
int Open_Buffer_Continue(Pointer handle, byte[] buffer, int size);
/**
* Open_Buffer_Continue_GoTo_Get
*/
long Open_Buffer_Continue_GoTo_Get(Pointer handle);
/**
* Open_Buffer_Finalize
*/
int Open_Buffer_Finalize(Pointer handle);
} }