原问题:https://stackoverflow.com/questions/2168472/media-information-extractor-for-java
使用MediaInfo获取音频,视频信息。
使用说明:
1.下载jna-4.2.1.jar
(http://download.csdn.net/download/csdnorder/9448389)
添加到项目build path
2.MediaInfo.dll
、 libmediainfo.so.0.0.0
放到同一个目录下,
也可以放在项目任意位置,只要修改MediaInfoLibrary.java下的代码即可
MediaInfoLibrary INSTANCE = (MediaInfoLibrary) Native.loadLibrary("你电脑MediaInfo.dll,libmediainfo.so.0.0.0路径", MediaInfoLibrary.class, singletonMap(OPTION_FUNCTION_MAPPER, new FunctionMapper() {
public String getFunctionName(NativeLibrary lib, Method method) {
// MediaInfo_New(), MediaInfo_Open() ...
return "MediaInfo_" + method.getName();
}
}));
(https://github.com/andersonkyle/mediainfo-java-api)
3.创建MediaInfo.java
、MediaInfoLibrary.java
import java.io.Closeable;
import java.io.File;
import java.util.ArrayList;
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;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
public class MediaInfo implements Closeable {
static {
// libmediainfo for linux depends on libzen
if (Platform.isLinux()) {
try {
// We need to load dependencies first, because we know where our native libs are (e.g. Java Web Start Cache).
// If we do not, the system will look for dependencies, but only in the library path.
NativeLibrary.getInstance("zen");
} catch (LinkageError e) {
Logger.getLogger(MediaInfo.class.getName()).warning("Failed to preload libzen");
}
}
}
private Pointer handle;
public MediaInfo() {
handle = MediaInfoLibrary.INSTANCE.New();
}
public synchronized boolean open(File file) {
return file.isFile() && MediaInfoLibrary.INSTANCE.Open(handle, new WString(file.getAbsolutePath())) > 0;
}
public synchronized String inform() {
return MediaInfoLibrary.INSTANCE.Inform(handle).toString();
}
public String option(String option) {
return option(option, "");
}
public synchronized String option(String option, String value) {
return MediaInfoLibrary.INSTANCE.Option(handle, new WString(option), new WString(value)).toString();
}
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, String parameter, InfoKind infoKind) {
return get(streamKind, streamNumber, parameter, infoKind, InfoKind.Name);
}
public synchronized String get(StreamKind streamKind, int streamNumber, String parameter, InfoKind infoKind, InfoKind searchKind) {
return MediaInfoLibrary.INSTANCE.Get(handle, streamKind.ordinal(), streamNumber, new WString(parameter), infoKind.ordinal(), searchKind.ordinal()).toString();
}
public String get(StreamKind streamKind, int streamNumber, int parameterIndex) {
return get(streamKind, streamNumber, parameterIndex, InfoKind.Text);
}
public synchronized String get(StreamKind streamKind, int streamNumber, int parameterIndex, InfoKind infoKind) {
return MediaInfoLibrary.INSTANCE.GetI(handle, streamKind.ordinal(), streamNumber, parameterIndex, infoKind.ordinal()).toString();
}
public synchronized int streamCount(StreamKind streamKind) {
return MediaInfoLibrary.INSTANCE.Count_Get(handle, streamKind.ordinal(), -1);
}
public synchronized int parameterCount(StreamKind streamKind, int streamNumber) {
return MediaInfoLibrary.INSTANCE.Count_Get(handle, streamKind.ordinal(), streamNumber);
}
public Map>> snapshot() {
Map>> mediaInfo = new EnumMap>>(StreamKind.class);
for (StreamKind streamKind : StreamKind.values()) {
int streamCount = streamCount(streamKind);
if (streamCount > 0) {
List
MediaInfoLibrary.java
import static java.util.Collections.singletonMap;
import java.lang.reflect.Method;
import com.sun.jna.FunctionMapper;
import com.sun.jna.Library;
import com.sun.jna.Native;
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() {
public String getFunctionName(NativeLibrary lib, Method method) {
// MediaInfo_New(), MediaInfo_Open() ...
return "MediaInfo_" + method.getName();
}
}));
/**
* Create a new handle.
*
* @return handle
*/
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
* @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
* @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.
*
* @param handle
* @return All details about a file in one string
*/
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
* @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...)
* @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.
*
* @param handle
* @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().
*
* @param handle
*/
void Close(Pointer handle);
/**
* Dispose of a handle created with New().
*
* @param handle
*/
void Delete(Pointer handle);
}
4.使用方法:下面代码演示获取视频,音频信息
import java.io.File;
public class TestMediaInfo
{
private static int i=0;
public static void main(String args[])
{
String fileName = "D:\\javaFx_workspace\\testVideo\\test1\\PIC_0781.MP4";
File file = new File(fileName);
MediaInfo info = new MediaInfo();
info.open(file);
String format = info.get(MediaInfo.StreamKind.Video, i, "Format",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String bitRate = info.get(MediaInfo.StreamKind.Video, i, "BitRate",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String frameRate = info.get(MediaInfo.StreamKind.Video, i, "FrameRate",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String width = info.get(MediaInfo.StreamKind.Video, i, "Width",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String audioBitrate = info.get(MediaInfo.StreamKind.Audio, i, "BitRate",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String audioChannels = info.get(MediaInfo.StreamKind.Audio, i, "Channels",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
String duration = info.get(MediaInfo.StreamKind.Video, i, "Duration",
MediaInfo.InfoKind.Text, MediaInfo.InfoKind.Name);
}
}