MediaInfo获取视频信息(帧率,时长,大小等)

原问题:https://stackoverflow.com/questions/2168472/media-information-extractor-for-java
使用MediaInfo获取音频,视频信息。
使用说明:
1.下载jna-4.2.1.jarhttp://download.csdn.net/download/csdnorder/9448389
添加到项目build path
2.MediaInfo.dlllibmediainfo.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)

image.png

3.创建MediaInfo.javaMediaInfoLibrary.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<StreamKind, List<Map<String, String>>> snapshot() {
        Map<StreamKind, List<Map<String, String>>> mediaInfo = new EnumMap<StreamKind, List<Map<String, String>>>(StreamKind.class);

        for (StreamKind streamKind : StreamKind.values()) {
            int streamCount = streamCount(streamKind);

            if (streamCount > 0) {
                List<Map<String, String>> streamInfoList = new ArrayList<Map<String, String>>(streamCount);

                for (int i = 0; i < streamCount; i++) {
                    streamInfoList.add(snapshot(streamKind, i));
                }

                mediaInfo.put(streamKind, streamInfoList);
            }
        }

        return mediaInfo;
    }


    public Map<String, String> snapshot(StreamKind streamKind, int streamNumber) {
        Map<String, String> streamInfo = new LinkedHashMap<String, String>();

        for (int i = 0, count = parameterCount(streamKind, streamNumber); i < count; i++) {
            String value = get(streamKind, streamNumber, i, InfoKind.Text);

            if (value.length() > 0) {
                streamInfo.put(get(streamKind, streamNumber, i, InfoKind.Name), value);
            }
        }

        return streamInfo;
    }

    public synchronized void close() {
        MediaInfoLibrary.INSTANCE.Close(handle);
    }


    public synchronized void dispose() {
        if (handle == null)
            return;

        // delete handle
        MediaInfoLibrary.INSTANCE.Delete(handle);
        handle = null;
    }


    @Override
    protected void finalize() {
        dispose();
    }


    public enum StreamKind {
        General,
        Video,
        Audio,
        Text,
        Chapters,
        Image,
        Menu;
    }


    public enum InfoKind {
        /**
         * Unique name of parameter.
         */
        Name,

        /**
         * Value of parameter.
         */
        Text,

        /**
         * Unique name of measure unit of parameter.
         */
        Measure,

        Options,

        /**
         * Translated name of parameter.
         */
        Name_Text,

        /**
         * Translated name of measure unit.
         */
        Measure_Text,

        /**
         * More information about the parameter.
         */
        Info,

        /**
         * How this parameter is supported, could be N (No), B (Beta), R (Read only), W
         * (Read/Write).
         */
        HowTo,

        /**
         * Domain of this piece of information.
         */
        Domain;
    }


    public static String version() {
        return staticOption("Info_Version");
    }


    public static String parameters() {
        return staticOption("Info_Parameters");
    }


    public static String codecs() {
        return staticOption("Info_Codecs");
    }


    public static String capacities() {
        return staticOption("Info_Capacities");
    }


    public static String staticOption(String option) {
        return staticOption(option, "");
    }


    public static String staticOption(String option, String value) {
        return MediaInfoLibrary.INSTANCE.Option(null, new WString(option), new WString(value)).toString();
    }

}

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);
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,657评论 18 139
  • 拿我的班内编号来当题目吧 无意之中知道了长投 带着目的性特意来参加了小白训练营 在训练营里面学到的东西我就不说了,...
    AnneWen阅读 174评论 0 1
  • 我会克服重重,去过我想过的生活,做我想成为的人。不论你在不在。 我希望你能在。
    林翊觉阅读 125评论 0 0
  • 1.- (void)URLSession:(NSURLSession *)session task:(NSURLS...
    廖马儿阅读 793评论 0 1