美文网首页
MediaInfo获取视频信息(帧率,时长,大小等)

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

作者: Mixqum | 来源:发表于2017-05-22 21:31 被阅读0次

    原问题: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);
            
        }
    }
    

    相关文章

      网友评论

          本文标题:MediaInfo获取视频信息(帧率,时长,大小等)

          本文链接:https://www.haomeiwen.com/subject/zemixxtx.html