* 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.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.ArrayList;
@ -10,6 +11,7 @@ import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;
@ -42,17 +44,50 @@ public class MediaInfo implements Closeable {
}
public synchronized boolean open(File file) {
if (!file.isFile())
if (!file.isFile()) {
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;
}
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() {
return MediaInfoLibrary.INSTANCE.Inform(handle).toString();
}

View File

@ -1,7 +1,5 @@
package net.filebot.mediainfo;
import static java.util.Collections.*;
import java.lang.reflect.Method;
@ -13,7 +11,6 @@ import com.sun.jna.NativeLibrary;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
interface MediaInfoLibrary extends Library {
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.
*
@ -33,28 +29,28 @@ interface MediaInfoLibrary extends Library {
*/
Pointer New();
/**
* Open a file and collect information about it (technical information and tags).
*
* @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
*/
int Open(Pointer handle, WString file);
/**
* Configure or get information about MediaInfo.
*
* @param handle
* @param option The name of option
* @param value The value of option
* @param option
* The name of option
* @param value
* The value of option
* @return Depends on the option: by default "" (nothing) means No, other means Yes
*/
WString Option(Pointer handle, WString option, WString value);
/**
* Get all details about a file.
*
@ -63,50 +59,52 @@ interface MediaInfoLibrary extends Library {
*/
WString Inform(Pointer handle);
/**
* Get a piece of information about a file (parameter is a string).
*
* @param handle
* @param streamKind Kind of stream (general, video, audio...)
* @param streamNumber Stream number in Kind of stream (first, second...)
* @param parameter Parameter you are looking for in the stream (Codec, width, bitrate...),
* in string format ("Codec", "Width"...)
* @param infoKind Kind of information you want about the parameter (the text, the measure,
* the help...)
* @param searchKind Where to look for the parameter
* @param streamKind
* Kind of stream (general, video, audio...)
* @param streamNumber
* Stream number in Kind of stream (first, second...)
* @param parameter
* Parameter you are looking for in the stream (Codec, width, bitrate...), in string format ("Codec", "Width"...)
* @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
*/
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).
*
* @param handle
* @param streamKind Kind of stream (general, video, audio...)
* @param streamNumber Stream number in Kind of stream (first, second...)
* @param parameter 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...)
* @param streamKind
* Kind of stream (general, video, audio...)
* @param streamNumber
* Stream number in Kind of stream (first, second...)
* @param parameter
* 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
*/
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
* information in this stream.
* Count of streams of a stream kind (StreamNumber not filled), or count of piece of information in this stream.
*
* @param handle
* @param streamKind Kind of stream (general, video, audio...)
* @param streamNumber Stream number in this kind of stream (first, second...)
* @param streamKind
* 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
*/
int Count_Get(Pointer handle, int streamKind, int streamNumber);
/**
* Close a file opened before with Open().
*
@ -114,7 +112,6 @@ interface MediaInfoLibrary extends Library {
*/
void Close(Pointer handle);
/**
* Dispose of a handle created with New().
*
@ -122,4 +119,24 @@ interface MediaInfoLibrary extends Library {
*/
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);
}