j最近在Adnroid开发用到了视频的读写操作,记录备忘一下:
都是借用android的med
1、保存TextView到mp4视频
2、读取MP4视频
/**
* 获取本地视频缩略图
* @param
* @return
*/
public void getVideoThumbnail() {
//获得当前时间
int sample = 5;//每秒5帧
Bitmap bitmap = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象
File file = new File(FileHelper.instance().getVideoFilePath(this));//实例化File对象,文件路径为/storage/sdcard/Movies/video.mp4
FileOutputStream fos = null;
File fileGaode = null;
if(file.exists()){
mmr.setDataSource(file.getAbsolutePath());//设置数据源为该文件对象指定的绝对路径
// 取得视频的长度(单位为毫秒)
String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
// 转换单位为秒)
int seconds = Integer.valueOf(time) / 1000;
int dowsample = seconds * sample;
// Bitmap bitmap = mmr.getFrameAtTime();//获得视频第一帧的Bitmap对象
// 得到每一秒时刻的bitmap比如第一秒,第二秒
for (int i = 1; i <= dowsample; i++) {
bitmap = mmr.getFrameAtTime(i * 1000 * 1000 / sample, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
String path = FileHelper.instance().getPicFilePath(this) + "/" + i + ".jpg";
//保存
if(bitmap!=null) {
try
{
fos = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (Exception e)
{
}
}
}
}else{
Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();//文件不存在时,弹出消息提示框
}
return bitmap;
}
public void saveBitmap2file(Bitmap bmp, String filename) {
if (bmp == null)
{
return;
}
String savePath;
String fileName = filename + ".jpeg";
savePath = FileHelper.instance().getSDcardPath();
File filePic = new File(savePath + fileName);
try {
if (!filePic.exists()) {
filePic.getParentFile().mkdirs();
filePic.createNewFile();
}
FileOutputStream fos = new FileOutputStream(filePic);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
// Toast.makeText(MainActivity., "保存成功,位置:" + filePic.getAbsolutePath(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
以下为测试在SD卡读写文件
{
// 在SD卡目录下创建文件
File file = new File(FileHelper.instance().getSDcardPath(), "mysdcard.txt");
Log.d(TAG, "file.exists():" + file.exists() + " file.getAbsolutePath():"+ file.getAbsolutePath());
if (file.exists()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 在SD卡目录下的文件,写入内容
FileWriter fw = null;
try {
fw = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.write("我sdcard内容.....");
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "SD卡写入内容完成...",Toast.LENGTH_LONG).show();
Log.d(TAG, "SD卡写入内容完成...");
// 读取SD卡文件里面的内容
FileReader fr = null;
try {
fr = new FileReader("/mnt/sdcard/mysdcard.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader r = new BufferedReader(fr);
String result = null;
try {
result = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "SD卡文件里面的内容:" + result);
Toast.makeText(MainActivity.this, "SD卡文件里面的内容...",Toast.LENGTH_LONG).show();
mBtnRecord.setText(result);
}
3、FFmpegMediaMetadataRetriever 也挺好用
https://github.com/wseemann/FFmpegMediaMetadataRetriever
使用
(1)在app下建立libs ,下载aar放进去
在app的build.gradle的andriod中加入
repositories {
flatDir {
dirs 'libs' // aar目录
}
我的:
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId ""
minSdkVersion 21
targetSdkVersion versionTarget
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions "
abiFilters "armeabi-v7a", "arm64-v8a"
arguments "-DANDROID_STL=gnustl_static" //该处添加gnustl_static,使得可读取opencv JNI的库文件。
}
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
}
buildTypes {
release {
zipAlignEnabled false //开启优化对齐
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
zipAlignEnabled false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets
{
main{
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
repositories {
flatDir {
dirs 'libs' // aar目录
}
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
(2)在app的build.gradle中
Add the following maven dependency to your project's build.gradle file:
dependencies {
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
}
Optionally, to support individual ABIs:
dependencies {
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi:1.0.14'
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86:1.0.14'
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-mips:1.0.14'
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86_64:1.0.14'
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-arm64-v8a:1.0.14'
}
本文介绍在Android开发中实现视频读写的具体方法,包括使用MediaMetadataRetriever从MP4视频中提取帧并保存为图片,以及利用FFmpegMediaMetadataRetriever进行更高效的操作。


被折叠的 条评论
为什么被折叠?



