Created
July 14, 2021 08:08
-
-
Save puke3615/68e8ce2df8c5e0b26849d2590dd26a78 to your computer and use it in GitHub Desktop.
VideoUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.media.MediaExtractor; | |
import android.media.MediaFormat; | |
import android.media.MediaMetadataRetriever; | |
import android.os.Environment; | |
import android.support.annotation.Nullable; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
/** | |
* @author puke | |
* @version 2020/06/28 | |
*/ | |
public abstract class VideoUtil { | |
public static VideoInfo parseVideoInfo(String videoPath) { | |
MediaMetadataRetriever retriever = null; | |
try { | |
retriever = new MediaMetadataRetriever(); | |
retriever.setDataSource(videoPath); | |
String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); | |
String width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); | |
String height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); | |
VideoInfo videoInfo = new VideoInfo(); | |
videoInfo.duration = Long.valueOf(duration); | |
videoInfo.width = Integer.valueOf(width); | |
videoInfo.height = Integer.valueOf(height); | |
return videoInfo; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (retriever != null) { | |
retriever.release(); | |
} | |
} | |
return null; | |
} | |
@Nullable | |
public static String getVideoCover(String videoPath) { | |
FileOutputStream is = null; | |
try { | |
MediaMetadataRetriever retriever = new MediaMetadataRetriever(); | |
retriever.setDataSource(videoPath); | |
Bitmap bitmap = retriever.getFrameAtTime(); | |
File saveDir = new File(Environment.getExternalStorageDirectory(), "bitmap"); | |
if (!saveDir.exists()) { | |
// noinspection ResultOfMethodCallIgnored | |
saveDir.mkdirs(); | |
} | |
File savePath = new File(saveDir, System.currentTimeMillis() + ".jpg"); | |
is = new FileOutputStream(savePath); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, is); | |
return savePath.getAbsolutePath(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} finally { | |
if (is != null) { | |
try { | |
is.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return null; | |
} | |
@Nullable | |
public static int[] getVideoSize(String videoPath) { | |
MediaExtractor extractor = null; | |
try { | |
extractor = new MediaExtractor(); | |
extractor.setDataSource(videoPath); | |
for (int i = 0; i < extractor.getTrackCount(); i++) { | |
MediaFormat mediaFormat = extractor.getTrackFormat(i); | |
String mime = mediaFormat.getString(MediaFormat.KEY_MIME); | |
if (mime != null && mime.startsWith("video/")) { | |
int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH); | |
int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); | |
return new int[]{width, height}; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (extractor != null) { | |
extractor.release(); | |
} | |
} | |
return null; | |
} | |
public static class VideoInfo { | |
public long duration; | |
public int width; | |
public int height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment