diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java index a045218..0911e46 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java @@ -387,8 +387,24 @@ public class VideoHandleActivity extends BaseActivity { private void convertGifInHighQuality(String gifPath, String videoPath, int startTime, int duration, int frameRate) { new Thread(() -> { 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(); - HighQualityGif highQualityGif = new HighQualityGif(); + HighQualityGif highQualityGif = new HighQualityGif(width, height, rotateDegree); boolean result = highQualityGif.convertGIF(gifPath, videoPath, startTime, duration, frameRate); Log.e(TAG, "convert gif result=" + result + "--time=" + (System.currentTimeMillis()-start)); mHandler.sendEmptyMessage(MSG_FINISH); diff --git a/app/src/main/java/com/frank/ffmpeg/gif/HighQualityGif.java b/app/src/main/java/com/frank/ffmpeg/gif/HighQualityGif.java index 0b3277b..808a757 100644 --- a/app/src/main/java/com/frank/ffmpeg/gif/HighQualityGif.java +++ b/app/src/main/java/com/frank/ffmpeg/gif/HighQualityGif.java @@ -29,13 +29,45 @@ public class HighQualityGif { 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 { if (TextUtils.isEmpty(filePath)) { return null; } String folderPath = Environment.getExternalStorageDirectory() + "/gif_frames/"; 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); File fileFolder = new File(folderPath); if (!fileFolder.exists() || fileFolder.listFiles() == null) {