Skip to content

Commit

Permalink
增加了图片的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
xiandanin committed Oct 17, 2018
1 parent 7ff8ae5 commit 1244ebe
Show file tree
Hide file tree
Showing 28 changed files with 1,278 additions and 194 deletions.
23 changes: 9 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# MediaMetadataRetrieverCompat
多媒体元数据兼容方案 - 支持获取视频缩略图、视频信息
多媒体元数据兼容方案 - 支持获取图片、视频、音频文件的媒体信息、视频缩略图

## __简介__
MediaMetadataRetrieverCompat 内部有两种实现(根据自身需求选择初始化方式)  

`FFmpegMediaMetadataRetriever`
       基于[FFmpegMediaMetadataRetriever](https://github.com/wseemann/FFmpegMediaMetadataRetriever)体积大但取帧速度快
       基于[FFmpegMediaMetadataRetriever](https://github.com/wseemann/FFmpegMediaMetadataRetriever)对视频资源有增强,但库体积较大

`MediaMetadataRetriever`
       基于原生API,不会增加apk体积但取帧慢
       基于原生API


## __示例apk__
![](screenshot/example-download.png)
![](screenshot/example-download_1.0.8.png)

## __效果演示__
![](screenshot/screenshot_auto.gif)
![](screenshot/screenshot_ffmpeg.gif)
![](screenshot/screenshot_android.gif)
![](screenshot/screenshot.gif)

## __快速开始__
```
//核心库 必选
implementation 'com.dyhdyh.compat.mmrc:media-metadata-retriever-compat:1.0.7'
implementation 'com.dyhdyh.compat.mmrc:media-metadata-retriever-compat:1.0.8'
//当需要FFmpegMediaMetadataRetriever时必选
implementation 'com.dyhdyh.remake:FFmpegMediaMetadataRetriever-java:1.0.14'
Expand Down Expand Up @@ -50,7 +48,7 @@ MediaMetadataRetrieverCompat mmrc = MediaMetadataRetrieverCompat.create();
//本地文件
mmrc.setDataSource(inputFile);
//网络视频(建议放在子线程)
//网络资源(需要放在子线程,网络操作将会交给API处理,请慎用,建议自行下载后设置File)
mmrc.setDataSource(url, headers);
//Uri
Expand All @@ -72,8 +70,8 @@ String framerate = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KE
...
```

## __获取本地视频缩略图__
耗时操作,请放在子线程
## __获取缩略图__
耗时操作,请放在子线程,获取到的缩略图会根据资源信息自动旋转

```
//获取第一帧原尺寸图片
Expand All @@ -84,7 +82,4 @@ mmrc.getFrameAtTime(timeUs, option);
//获取指定位置指定宽高的缩略图
mmrc.getScaledFrameAtTime(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height);
//获取指定位置指定宽高并且旋转的缩略图
mmrc.getScaledFrameAtTime(timeUs, MediaMetadataRetrieverCompat.OPTION_CLOSEST, width, height, rotate);
```
6 changes: 3 additions & 3 deletions config.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ext {
android =
[
versionCode : 8,
versionName : "1.0.7",
versionCode : 9,
versionName : "1.0.8",

compileSdkVersion: 26,
buildToolsVersion: "26.0.2",
Expand All @@ -21,7 +21,7 @@ ext {
//项目git地址
gitUrl : "https://github.com/dengyuhan/MediaMetadataRetrieverCompat.git",
//项目描述
description: "多媒体元数据兼容方案 - 支持获取视频缩略图、视频信息",
description: "多媒体元数据兼容方案 - 支持获取图片、视频、音频文件的媒体信息、视频缩略图",
//开发者的一些基本信息
authorId : "dengyuhan",
authorName : "dengyuhan",
Expand Down
1 change: 1 addition & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ dependencies {

implementation 'com.dyhdyh.remake:FFmpegMediaMetadataRetriever-java:1.0.14'
implementation 'com.dyhdyh.remake:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'
implementation 'com.android.support:exifinterface:' + rootProject.ext.android.supportVersion
compile project(':media-metadata-retriever-compat')
}
Binary file added example/src/main/assets/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/src/main/assets/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public class MainActivity extends AppCompatActivity implements RadioGroup.OnChec
RecyclerView rv;
View layout_info;

private File testFile;

private ThumbnailAdapter mThumbnailAdapter;
private MediaMetadataRetrieverCompat mmrc;

private Disposable mThumbnailDisposable;

private SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");

@Override
Expand All @@ -68,15 +68,15 @@ protected void onCreate(Bundle savedInstanceState) {

AssetsManager.copyAsset(this, new AssetFile(), getExternalCacheDir());

testFile = new File(getExternalCacheDir(), "test.mp4");

mmrc = MediaMetadataRetrieverCompat.create();
//mmrc = MediaMetadataRetrieverCompat.create(MediaMetadataRetrieverCompat.RETRIEVER_FFMPEG);
//mmrc = MediaMetadataRetrieverCompat.create(MediaMetadataRetrieverCompat.RETRIEVER_ANDROID);


}

public void clickMediaMetadata(View v) {
final String path = TextUtils.isEmpty(ed.getText()) ? testFile.getAbsolutePath() : ed.getText().toString();
final String path = TextUtils.isEmpty(ed.getText()) ? new File(getExternalCacheDir(), "test.mp4").getAbsolutePath() : ed.getText().toString();

//这里示例用子线程 实际开发中根据需求
Observable.create(new ObservableOnSubscribe<String>() {
Expand All @@ -89,14 +89,6 @@ public void subscribe(@NonNull ObservableEmitter<String> s) throws Exception {
} else {
mmrc.setDataSource(new File(path));
}
//获取缩略图
long duration = mmrc.extractMetadataLong(MediaMetadataRetrieverCompat.METADATA_KEY_DURATION);
int width = mmrc.extractMetadataInt(MediaMetadataRetrieverCompat.METADATA_KEY_VIDEO_WIDTH);
int height = mmrc.extractMetadataInt(MediaMetadataRetrieverCompat.METADATA_KEY_VIDEO_HEIGHT);
int thumbnailCount = (int) Math.max(1, Math.min(10, duration / 1000));
int thumbnailWidth = getResources().getDimensionPixelSize(R.dimen.thumbnail_size);
final int thumbnailHeight = (int) ((float) thumbnailWidth / width * height);
buildThumbnail(thumbnailCount, thumbnailWidth, thumbnailHeight);
s.onNext(buildMetadataInfo());
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -114,6 +106,15 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(String s) {
//获取缩略图
long duration = mmrc.extractMetadataLong(MediaMetadataRetrieverCompat.METADATA_KEY_DURATION);
int width = mmrc.extractMetadataInt(MediaMetadataRetrieverCompat.METADATA_KEY_WIDTH);
int height = mmrc.extractMetadataInt(MediaMetadataRetrieverCompat.METADATA_KEY_HEIGHT);
int thumbnailCount = (int) Math.max(1, Math.min(10, duration / 1000));
int thumbnailWidth = getResources().getDimensionPixelSize(R.dimen.thumbnail_size);
final int thumbnailHeight = (int) ((float) thumbnailWidth / width * height);
buildThumbnail(thumbnailCount, thumbnailWidth, thumbnailHeight);

LoadingBar.cancel(layout_info);
tv.setText(s);
}
Expand All @@ -132,12 +133,12 @@ private String buildMetadataInfo() {
sb.append("API类型:");
sb.append(mmrc.getMediaMetadataRetriever().getClass().getSimpleName());
sb.append("\n");
String width = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_VIDEO_WIDTH);
String width = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_WIDTH);
if (width != null) {
sb.append("\n宽:");
sb.append(width);
}
String height = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_VIDEO_HEIGHT);
String height = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_HEIGHT);
if (height != null) {
sb.append("\n高:");
sb.append(height);
Expand All @@ -150,7 +151,7 @@ private String buildMetadataInfo() {
sb.append(duration);
sb.append("毫秒)");
}
String rotation = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_VIDEO_ROTATION);
String rotation = mmrc.extractMetadata(MediaMetadataRetrieverCompat.METADATA_KEY_ROTATION);
if (rotation != null) {
sb.append("\n旋转角度:");
sb.append(rotation);
Expand Down Expand Up @@ -189,7 +190,13 @@ private String buildMetadataInfo() {
}

private void buildThumbnail(final int count, final int width, final int height) {
mThumbnailAdapter = null;
if (mThumbnailDisposable != null && mThumbnailDisposable.isDisposed()) {
mThumbnailDisposable.dispose();
}

mThumbnailAdapter = new ThumbnailAdapter(count);//每秒取1帧
rv.setAdapter(mThumbnailAdapter);

//取帧是耗时的操作,需要放在子线程
Observable.create(new ObservableOnSubscribe<ThumbnailBitmap>() {
@Override
Expand All @@ -211,13 +218,13 @@ public void subscribe(@NonNull ObservableEmitter<ThumbnailBitmap> s) throws Exce
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SimpleObserver<ThumbnailBitmap>() {
@Override
public void onSubscribe(Disposable d) {
mThumbnailDisposable = d;
}

@Override
public void onNext(ThumbnailBitmap bitmap) {
if (mThumbnailAdapter == null) {
mThumbnailAdapter = new ThumbnailAdapter(count);//每秒取1帧
rv.setAdapter(mThumbnailAdapter);
}
//刷新adapter
mThumbnailAdapter.setThumbnail(bitmap.getIndex(), bitmap.getBitmap());
}
Expand Down Expand Up @@ -282,4 +289,14 @@ public void clickAudio(MenuItem item) {
ed.setText(new File(getExternalCacheDir(), "test_audio.mp3").getAbsolutePath());
clickMediaMetadata(null);
}

public void clickJpg(MenuItem item) {
ed.setText(new File(getExternalCacheDir(), "test.jpg").getAbsolutePath());
clickMediaMetadata(null);
}

public void clickGif(MenuItem item) {
ed.setText(new File(getExternalCacheDir(), "test.gif").getAbsolutePath());
clickMediaMetadata(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(Holder holder, int position) {
if (bitmaps[position] == null) {
holder.iv_thumbnail.setImageResource(R.mipmap.ic_launcher);
} else {
holder.iv_thumbnail.setImageBitmap(bitmaps[position]);
}
holder.iv_thumbnail.setImageBitmap(bitmaps[position]);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickMediaMetadata"
android:text="获取视频信息/缩略图" />
android:text="获取媒体数据/缩略图" />

<RelativeLayout
android:id="@+id/layout_info"
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/item_thumbnail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
android:layout_height="wrap_content">

<ImageView
android:id="@+id/iv_thumbnail"
android:scaleType="centerCrop"
android:id="@+id/iv_thumbnail"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size" />
</RelativeLayout>
6 changes: 6 additions & 0 deletions example/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<item
android:onClick="clickAudio"
android:title="测试音频" />
<item
android:onClick="clickGif"
android:title="测试GIF" />
<item
android:onClick="clickJpg"
android:title="测试JPG" />
<item
android:onClick="clickOnlineVideo"
android:title="测试网络视频" />
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

<dimen name="thumbnail_size">100dp</dimen>
<dimen name="thumbnail_size">150dp</dimen>
</resources>
2 changes: 1 addition & 1 deletion example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<string name="app_name">MediaMetadataRetrieverCompat</string>
<string name="hint_video_path">输入视频路径,支持本地文件和网络视频</string>
<string name="hint_video_path">文件路径,支持本地文件和网络文件</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.graphics.Bitmap;
import android.net.Uri;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.Map;

/**
Expand All @@ -13,7 +15,7 @@
*/
public interface IMediaMetadataRetriever {

void setDataSource(String path);
void setDataSource(File inputFile) throws FileNotFoundException;

void setDataSource(String uri, Map<String, String> headers);

Expand All @@ -33,7 +35,7 @@ public interface IMediaMetadataRetriever {

byte[] getEmbeddedPicture();

String extractMetadata(String keyCode);
String extractMetadata(int keyCode);

void release();
}
Loading

0 comments on commit 1244ebe

Please sign in to comment.