2022年7月26日

Android Get Video Info(取得影片資訊)

 



import android.content.Context;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.util.Log;
import android.util.Pair;
import java.io.IOException;

public class Video_Info {

private static String TAG="TTT";

public int video_w;
public int video_h;
public int degree;
public int frames;
public int bitrate;
public int frame_interval;
public long duration;


public Video_Info(){
duration=0;
frame_interval=0;
bitrate=0;
frames=0;
degree=0;
video_w=1;
video_h=1;


}

public void show_log(){


Log.e(TAG,"Duration "+this.duration);
Log.e(TAG,"Frame_interval "+this.frame_interval);
Log.e(TAG,"Bitrate "+this.bitrate);
Log.e(TAG,"Degree "+this.degree);
Log.e(TAG,"Frames "+this.frames);
Log.e(TAG,"Video_w "+this.video_w);
Log.e(TAG,"Video_h "+this.video_h);

}

public void set_video(Context context,Uri video_uri){
this.duration=get_duration(context,video_uri);
this.frame_interval=get_frame_interval(context,video_uri);
this.bitrate=get_bitrate(context,video_uri);
this.degree=get_rotatin(context,video_uri);
this.frames=getFramerate(context,video_uri);

try {
Pair<Integer, Integer> size = getVideoSize(context, video_uri);

this.video_w=size.first;
this.video_h=size.second;
}catch (Exception e){

}
}


public static long get_duration(Context context,Uri uri){


long duration=0;
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(context,uri,null);
MediaFormat format = extractor.getTrackFormat(selectTrack(extractor, false));
duration= format.getLong(MediaFormat.KEY_DURATION);

}catch (Exception e){

// Log.e("TTT",e.getMessage());

}

return duration;

}

public static int get_frame_interval(Context context,Uri uri){




int interval=1;
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(context,uri,null);
MediaFormat format = extractor.getTrackFormat(selectTrack(extractor, false));
if(format.containsKey(MediaFormat.KEY_I_FRAME_INTERVAL))
interval= format.getInteger(MediaFormat.KEY_I_FRAME_INTERVAL);

}catch (Exception e){
// Log.e("TTT",e.getMessage());
}

return interval;
}
public static int get_bitrate(Context context, Uri uri){

MediaMetadataRetriever m = new MediaMetadataRetriever();
int bitrate=0;
try {

m.setDataSource(context,uri);
bitrate= Integer.valueOf(m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
}catch (Exception e){

// Log.e("TTT",e.getMessage());
}

return bitrate;

}
public static int get_rotatin(Context context,Uri uri){

MediaExtractor extractor = new MediaExtractor();
int degree=0;
try {
extractor.setDataSource(context,uri,null);
MediaFormat format = extractor.getTrackFormat(selectTrack(extractor, false));
degree= format.getInteger(MediaFormat.KEY_ROTATION);
}catch (Exception e){


}

return degree;

}

public static int getFramerate(Context context,Uri uri){

MediaExtractor extractor = new MediaExtractor();
int frameRate = 24; //may be default

try {
extractor.setDataSource(context,uri,null);
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//Release stuff
extractor.release();
}
return frameRate;

}
public static Pair<Integer,Integer> getVideoSize(Context context,Uri uri) throws IOException {

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(context,uri,null);
MediaFormat format = extractor.getTrackFormat(selectTrack(extractor, false));
int width = format.getInteger(MediaFormat.KEY_WIDTH);
int height = format.getInteger(MediaFormat.KEY_HEIGHT);
return new Pair<>(width,height);
}



private static int selectTrack(MediaExtractor extractor, boolean audio) {
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (audio) {
if (mime.startsWith("audio/")) {
return i;
}
} else {
if (mime.startsWith("video/")) {
return i;
}
}
}
return -5;
}


}

沒有留言:

張貼留言