select the suitable output resolution

select the suitable output resolution
pull/166/head
xufulong 4 years ago
parent 5f75d3831c
commit 0b8e200bfd
  1. 18
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  2. 34
      app/src/main/java/com/frank/ffmpeg/gif/HighQualityGif.java

@ -387,8 +387,24 @@ public class VideoHandleActivity extends BaseActivity {
private void convertGifInHighQuality(String gifPath, String videoPath, int startTime, int duration, int frameRate) { private void convertGifInHighQuality(String gifPath, String videoPath, int startTime, int duration, int frameRate) {
new Thread(() -> { new Thread(() -> {
mHandler.sendEmptyMessage(MSG_BEGIN); mHandler.sendEmptyMessage(MSG_BEGIN);
int width=0, height=0;
int rotateDegree = 0;
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(videoPath);
String mWidth = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
String mHeight = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
width = Integer.valueOf(mWidth);
height = Integer.valueOf(mHeight);
String rotate = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
rotateDegree = Integer.valueOf(rotate);
retriever.release();
Log.e(TAG, "retrieve width=" + width + "--height=" + height + "--rotate=" + rotate);
} catch (Exception e) {
Log.e(TAG, "retrieve error=" + e.toString());
}
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
HighQualityGif highQualityGif = new HighQualityGif(); HighQualityGif highQualityGif = new HighQualityGif(width, height, rotateDegree);
boolean result = highQualityGif.convertGIF(gifPath, videoPath, startTime, duration, frameRate); boolean result = highQualityGif.convertGIF(gifPath, videoPath, startTime, duration, frameRate);
Log.e(TAG, "convert gif result=" + result + "--time=" + (System.currentTimeMillis()-start)); Log.e(TAG, "convert gif result=" + result + "--time=" + (System.currentTimeMillis()-start));
mHandler.sendEmptyMessage(MSG_FINISH); mHandler.sendEmptyMessage(MSG_FINISH);

@ -29,13 +29,45 @@ public class HighQualityGif {
private final static int TARGET_HEIGHT = 180; private final static int TARGET_HEIGHT = 180;
private int mWidth;
private int mHeight;
private int mRotateDegree;
public HighQualityGif(int width, int height, int rotateDegree) {
mWidth = width;
mHeight = height;
mRotateDegree = rotateDegree;
}
private int chooseWidth(int width, int height) {
if (width <= 0 || height <= 0) {
return TARGET_WIDTH;
}
int target;
if (mRotateDegree == 0 || mRotateDegree == 180) {//landscape
if (width > TARGET_WIDTH) {
target = TARGET_WIDTH;
} else {
target = width;
}
} else {//portrait
if (height > TARGET_HEIGHT) {
target = TARGET_HEIGHT;
} else {
target = height;
}
}
return target;
}
private byte[] generateGif(String filePath, int startTime, int duration, int frameRate) throws IllegalArgumentException { private byte[] generateGif(String filePath, int startTime, int duration, int frameRate) throws IllegalArgumentException {
if (TextUtils.isEmpty(filePath)) { if (TextUtils.isEmpty(filePath)) {
return null; return null;
} }
String folderPath = Environment.getExternalStorageDirectory() + "/gif_frames/"; String folderPath = Environment.getExternalStorageDirectory() + "/gif_frames/";
FileUtil.deleteFolder(folderPath); FileUtil.deleteFolder(folderPath);
String[] commandLine = FFmpegUtil.videoToImageWithScale(filePath, startTime, duration, frameRate, TARGET_WIDTH, folderPath); int targetWidth = chooseWidth(mWidth, mHeight);
String[] commandLine = FFmpegUtil.videoToImageWithScale(filePath, startTime, duration, frameRate, targetWidth, folderPath);
FFmpegCmd.executeSync(commandLine); FFmpegCmd.executeSync(commandLine);
File fileFolder = new File(folderPath); File fileFolder = new File(folderPath);
if (!fileFolder.exists() || fileFolder.listFiles() == null) { if (!fileFolder.exists() || fileFolder.listFiles() == null) {

Loading…
Cancel
Save