1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a BSD-style license that can
4  *  be found in the License.html file in the root of the source tree.
5  */
6 
7 class MediaInfo
8 {
9     static
10     {
11         // libmediainfo for linux depends on libzen
12         String os=System.getProperty("os.name");
13         if (os!=null && !os.toLowerCase().startsWith("windows") && !os.toLowerCase().startsWith("mac"))
14         {
15            try
16            {
17                System.loadLibrary("zen");
18            }
19            catch (LinkageError e)
20            {
21            }
22         }
23 
24         try
25         {
26             System.loadLibrary("mediainfo");
27         }
28         catch (LinkageError e)
29         {
30             throw new UnsatisfiedLinkError("Unable to load library 'mediainfo'");
31         }
32     }
33 
34     public long mi = 0; // Pointer to MediaInfo instance, DO NOT RENAME without editing the corresponding variable in C++ source
35 
36     // Constructor
MediaInfo()37     public MediaInfo()
38     {
39         try
40         {
41           mi = Init();
42         }
43         catch (LinkageError e)
44         {
45           throw new UnsatisfiedLinkError("Library 'mediainfo' found but its JNI interface is missing");
46         }
47     }
48 
49     public enum StreamKind {
50         General,
51         Video,
52         Audio,
53         Text,
54         Other,
55         Image,
56         Menu;
57     }
58 
59     //Enums
60     public enum InfoKind {
61         /**
62          * Unique name of parameter.
63          */
64         Name,
65 
66         /**
67          * Value of parameter.
68          */
69         Text,
70 
71         /**
72          * Unique name of measure unit of parameter.
73          */
74         Measure,
75 
76         Options,
77 
78         /**
79          * Translated name of parameter.
80          */
81         Name_Text,
82 
83         /**
84          * Translated name of measure unit.
85          */
86         Measure_Text,
87 
88         /**
89          * More information about the parameter.
90          */
91         Info,
92 
93         /**
94          * How this parameter is supported, could be N (No), B (Beta), R (Read only), W
95          * (Read/Write).
96          */
97         HowTo,
98 
99         /**
100          * Domain of this piece of information.
101          */
102         Domain;
103     }
104 
105     public enum Status {
106         None        (0x00),
107         Accepted    (0x01),
108         Filled      (0x02),
109         Updated     (0x04),
110         Finalized   (0x08);
111 
112         private int value;
Status(int value)113         private Status(int value) {this.value = value;}
getValue(int value)114         public int getValue(int value) {return value;}
115     }
116 
dispose()117      public void dispose()
118     {
119         Destroy();
120         mi = 0;
121     }
122 
123     @Override
finalize()124     protected void finalize() throws Throwable
125     {
126         Destroy();
127     }
128 
Init()129     public native long Init();
130 
Destroy()131     public native int Destroy();
132 
133     //File
134     /**
135      * Open a file and collect information about it (technical information and tags).
136      *
137      * @param file full name of the file to open
138      * @return 1 if file was opened, 0 if file was not not opened
139      */
Open(String name)140     public native int Open(String name);
141 
Open_Buffer_Init(long fileSize, long fileOffset)142     public native int Open_Buffer_Init(long fileSize, long fileOffset);
143 
144     /**
145      *  Open a stream and collect information about it (technical information and tags) (By buffer, Continue)
146 
147      * @param buffer pointer to the stream
148      * @param size Count of bytes to read
149      * @return a bitfield
150                 bit 0: Is Accepted (format is known)
151                 bit 1: Is Filled (main data is collected)
152                 bit 2: Is Updated (some data have beed updated, example: duration for a real time MPEG-TS stream)
153                 bit 3: Is Finalized (No more data is needed, will not use further data)
154                 bit 4-15: Reserved
155                 bit 16-31: User defined
156      */
Open_Buffer_Continue(byte[] buffer, long bufferSize)157     public native int Open_Buffer_Continue(byte[] buffer, long bufferSize);
158 
159 
Open_Buffer_Continue_GoTo_Get()160     public native long Open_Buffer_Continue_GoTo_Get();
161 
Open_Buffer_Finalize()162     public native long Open_Buffer_Finalize();
163 
164     /**
165      * Close a file opened before with Open().
166      *
167      */
Close()168     public native int Close();
169 
170     //Information
171     /**
172      * Get all details about a file.
173      *
174      * @return All details about a file in one string
175      */
Inform()176     public native String Inform();
177 
GetI(int streamKind, int streamNumber, int parameter, int infoKind)178     private native String GetI(int streamKind, int streamNumber, int parameter, int infoKind);
179 
GetS(int streamKind, int streamNumber, String parameter, int infoKind, int searchKind)180     private native String GetS(int streamKind, int streamNumber, String parameter, int infoKind, int searchKind);
181 
182     /**
183      * Get a piece of information about a file (parameter is an integer).
184      *
185 
186      * @param StreamKind Kind of Stream (general, video, audio...)
187      * @param StreamNumber Stream number in Kind of Stream (first, second...)
188      * @param parameter Parameter you are looking for in the Stream (Codec, width, bitrate...),
189      *            in integer format (first parameter, second parameter...)
190      * @return a string about information you search, an empty string if there is a problem
191      */
Get(StreamKind streamKind, int streamNumber, int parameter)192     public String Get(StreamKind streamKind, int streamNumber, int parameter)
193     {
194         return GetI(streamKind.ordinal(), streamNumber, parameter, InfoKind.Text.ordinal());
195     }
196 
197     /**
198      * Get a piece of information about a file (parameter is an integer).
199      *
200 
201      * @param StreamKind Kind of Stream (general, video, audio...)
202      * @param StreamNumber Stream number in Kind of Stream (first, second...)
203      * @param parameter Parameter you are looking for in the Stream (Codec, width, bitrate...),
204      *            in integer format (first parameter, second parameter...)
205      * @param infoKind Kind of information you want about the parameter (the text, the measure,
206      *            the help...)
207      * @return a string about information you search, an empty string if there is a problem
208      */
Get(StreamKind streamKind, int streamNumber, int parameter, InfoKind infoKind)209     public String Get(StreamKind streamKind, int streamNumber, int parameter, InfoKind infoKind)
210     {
211         return GetI(streamKind.ordinal(), streamNumber, parameter, infoKind.ordinal());
212     }
213 
214     /**
215      * Get a piece of information about a file (parameter is a string).
216      *
217      * @param StreamKind Kind of Stream (general, video, audio...)
218      * @param StreamNumber Stream number in Kind of Stream (first, second...)
219      * @param parameter Parameter you are looking for in the Stream (Codec, width, bitrate...),
220      *            in string format ("Codec", "Width"...)
221      * @param infoKind Kind of information you want about the parameter (the text, the measure,
222      *            the help...)
223      * @return a string about information you search, an empty string if there is a problem
224      */
Get(StreamKind streamKind, int streamNumber, String parameter, InfoKind infoKind)225     public String Get(StreamKind streamKind, int streamNumber, String parameter, InfoKind infoKind)
226     {
227         return GetS(streamKind.ordinal(), streamNumber, parameter, infoKind.ordinal(), InfoKind.Name.ordinal());
228     }
229 
230     /**
231      * Get a piece of information about a file (parameter is a string).
232      *
233      * @param StreamKind Kind of Stream (general, video, audio...)
234      * @param StreamNumber Stream number in Kind of Stream (first, second...)
235      * @param parameter Parameter you are looking for in the Stream (Codec, width, bitrate...),
236      *            in string format ("Codec", "Width"...)
237      * @param infoKind Kind of information you want about the parameter (the text, the measure,
238      *            the help...)
239      * @param searchKind Where to look for the parameter
240      * @return a string about information you search, an empty string if there is a problem
241      */
Get(StreamKind streamKind, int streamNumber, String parameter, InfoKind infoKind, InfoKind searchKind)242     public String Get(StreamKind streamKind, int streamNumber, String parameter, InfoKind infoKind, InfoKind searchKind)
243     {
244         return GetS(streamKind.ordinal(), streamNumber, parameter, infoKind.ordinal(), searchKind.ordinal());
245     }
246 
247     //Options
248     /**
249      * Configure or get information about MediaInfo.
250      *
251      * @param Option The name of option
252      * @param Value The value of option
253      * @return Depends on the option: by default "" (nothing) means No, other means Yes
254      */
Option(String option, String value)255     public native String Option(String option, String value);
256 
257     /**
258      * Configure or get information about MediaInfo.
259      *
260      * @param Option The name of option
261      * @return Depends on the option: by default "" (nothing) means No, other means Yes
262      */
Option(String option)263     public String Option(String option)
264     {
265         return Option(option, "");
266     }
267 
268     //Options
269     /**
270      * Configure or get information about MediaInfo. (static version)
271      *
272      * @param Option The name of option
273      * @param Value The value of option
274      * @return Depends on the option: by default "" (nothing) means No, other means Yes
275      */
Option_Static(String option, String value)276     public static String Option_Static(String option, String value)
277     {
278         return (new MediaInfo()).Option(option, value);
279     }
280 
281     /**
282      * Configure or get information about MediaInfo. (static version)
283      *
284      * @param Option The name of option
285      * @return Depends on the option: by default "" (nothing) means No, other means Yes
286      */
Option_Static(String option)287     public static String Option_Static(String option)
288     {
289         return Option_Static(option, "");
290     }
291 
292     /**
293      * Gets the state of the library
294      * @return                                state of the library (between 0 and 10000)
295     */
State_Get()296     public native int State_Get();
297 
Count_Get(int streamKind, int streamNumber)298     private native int Count_Get(int streamKind, int streamNumber);
299 
300     /**
301      * Count of Streams of a Stream kind (StreamNumber not filled), or count of piece of
302      * information in this Stream.
303      *
304 
305      * @param StreamKind Kind of Stream (general, video, audio...)
306      * @return number of Streams of the given Stream kind
307      */
Count_Get(StreamKind streamKind)308     public int Count_Get(StreamKind streamKind)
309     {
310         return Count_Get(streamKind.ordinal(), -1);
311     }
312 
313     /**
314      * Count of Streams of a Stream kind (StreamNumber not filled), or count of piece of
315      * information in this Stream.
316      *
317      * @param StreamKind Kind of Stream (general, video, audio...)
318      * @param StreamNumber Stream number in this kind of Stream (first, second...)
319      * @return number of Streams of the given Stream kind
320      */
Count_Get(StreamKind streamKind, int streamNumber)321     public int Count_Get(StreamKind streamKind, int streamNumber)
322     {
323         return Count_Get(streamKind.ordinal(), streamNumber);
324     }
325 }
326