1.获取去视频信息(时长,视频宽度 长度等),以及获取视频的某一帧(下面选择进度调用)
android 原生 MediaMetadataRetriever

    Mediametadataretriever类提供了一个统一的接口取回帧和取回从一个输入媒体文件中的元数据

用法:
1.初始化
public ExtractVideoInfoUtil(String path) {
if (TextUtils.isEmpty(path)) {
throw new RuntimeException(“path must be not null !”);
}
File file = new File(path);
if (!file.exists()) {
throw new RuntimeException(“path file not exists !”);
}
mMetadataRetriever = new MediaMetadataRetriever();
mMetadataRetriever.setDataSource(file.getAbsolutePath());
}
}
2.获取视频信息

public MediaMetadataRetriever getMetadataRetriever() {
    return mMetadataRetriever;
}

public int getVideoWidth() {
    String w = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
    int width = -1;
    if (!TextUtils.isEmpty(w)) {
        width = Integer.valueOf(w);
    }
    return width;
}

public int getVideoHeight() {
    String h = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
    int height = -1;
    if (!TextUtils.isEmpty(h)) {
        height = Integer.valueOf(h);
    }
    return height;
}

/**
 * 获取视频的典型的一帧图片,不耗时
 *
 * @return Bitmap
 */
public Bitmap extractFrame() {
    return mMetadataRetriever.getFrameAtTime();
}

/**
 * 获取视频某一帧,不一定是关键帧
 *
 * @param timeMs 毫秒
 */
public Bitmap extractFrame(long timeMs) {
    //第一个参数是传入时间,只能是us(微秒)
    //OPTION_CLOSEST ,在给定的时间,检索最近一个帧,这个帧不一定是关键帧。
    //OPTION_CLOSEST_SYNC   在给定的时间,检索最近一个同步与数据源相关联的的帧(关键帧)
    //OPTION_NEXT_SYNC 在给定时间之后检索一个同步与数据源相关联的关键帧。
    //OPTION_PREVIOUS_SYNC  顾名思义,同上

// Bitmap bitmap = mMetadataRetriever.getFrameAtTime(timeMs * 1000, MediaMetadataRetriever.OPTION_CLOSEST);
Bitmap bitmap = null;
for (long i = timeMs; i < fileLength; i += 1000) {
bitmap = mMetadataRetriever.getFrameAtTime(i * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
if (bitmap != null) {
break;
}
}
return bitmap;
}

/***
 * 获取视频的长度时间
 *
 * @return String 毫秒
 */
public String getVideoLength() {
    String len = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    fileLength = TextUtils.isEmpty(len) ? 0 : Long.valueOf(len);
    return len;
}

/**
 * 获取视频旋转角度
 *
 * @return
 */
public int getVideoDegree() {
    int degree = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        String degreeStr = mMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
        if (!TextUtils.isEmpty(degreeStr)) {
            degree = Integer.valueOf(degreeStr);
        }
    }
    return degree;
}

public void release() {
    if (mMetadataRetriever != null) {
        mMetadataRetriever.release();
    }
}

2.视频裁剪
方案1:使用框架mp4parser(缺点只支持MP4)

    需要aspectjrt-1.7.3.jar 和isoparser-1.0.6.jar     

/**
* 裁剪视频(异步操作)
*
* @param src 源文件
* @param dest 输出地址
* @param startSec 开始时间
* @param endSec 结束时间
*/
public static Observable cutVideo(final String src, final String dest, final double startSec, final double endSec) {

    return Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> emitter) {
            try {
                double startSecond = startSec;
                double endSecond = endSec;
                //构造一个movie对象
                Movie movie = MovieCreator.build(src);
                List<Track> tracks = movie.getTracks();
                movie.setTracks(new ArrayList<Track>());

                boolean timeCorrected = false;
                // Here we try to find a track that has sync samples. Since we can only start decoding
                // at such a sample we SHOULD make sure that the start of the new fragment is exactly
                // such a frame
                for (Track track : tracks) {
                    if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
                        if (timeCorrected) {
                            // This exception here could be a false positive in case we have multiple tracks
                            // with sync samples at exactly the same positions. E.g. a single movie containing
                            // multiple qualities of the same video (Microsoft Smooth Streaming file)
                            throw new RuntimeException(
                                "The startTime has already been corrected by another track with SyncSample. Not Supported.");
                        }
                        //矫正开始时间
                        startSecond = correctTimeToSyncSample(track, startSecond, false);
                        //矫正结束时间
                        endSecond = correctTimeToSyncSample(track, endSecond, true);
                        //true,false表示短截取;false,true表示长截取
                        timeCorrected = true;
                    }
                }

                //裁剪后的位置   startSecond:299400, endSecond:309390
                //矫正后的位置   startSecond:291.3327083333511, endSecond:313.18787500003214
                Log.e(TAG, "startSecond:" + startSecond + ", endSecond:" + endSecond);

                //fix bug: 部分视频矫正过后会超出10s,这里进行强制限制在10s内
                if (endSecond - startSecond > 10) {
                    int duration = (int) (endSec - startSec);
                    endSecond = startSecond + duration;
                }
                //fix bug: 部分视频裁剪后endSecond=0.0,导致播放失败
                if (endSecond == 0.0) {
                    int duration = (int) (endSec - startSec);
                    endSecond = startSecond + duration;
                }

                for (Track track : tracks) {
                    long currentSample = 0;
                    double currentTime = 0;
                    double lastTime = -1;
                    long startSample = -1;
                    long endSample = -1;

                    for (int i = 0; i < track.getSampleDurations().length; i++) {
                        long delta = track.getSampleDurations()[i];

                        if (currentTime > lastTime && currentTime <= startSecond) {
                            // current sample is still before the new starttime
                            startSample = currentSample;
                        }
                        if (currentTime > lastTime && currentTime <= endSecond) {
                            // current sample is after the new start time and still before the new endtime
                            endSample = currentSample;
                        }

                        lastTime = currentTime;
                        //计算出某一帧的时长 = 采样时长 / 时间长度
                        currentTime +=
                            (double) delta / (double) track.getTrackMetaData().getTimescale();
                        //这里就是帧数(采样)加一
                        currentSample++;
                    }
                    //在这里,裁剪是根据关键帧进行裁剪的,而不是指定的开始时间和结束时间
                    //startSample:2453, endSample:2846   393
                    //startSample:4795, endSample:5564   769
                    Log.e(TAG, "startSample:" + startSample + ", endSample:" + endSample);
                    movie.addTrack(new CroppedTrack(track, startSample, endSample));

                    Container out = new DefaultMp4Builder().build(movie);
                    FileOutputStream fos = new FileOutputStream(String.format(dest));
                    FileChannel fc = fos.getChannel();
                    out.writeContainer(fc);

                    fc.close();
                    fos.close();
                }

                emitter.onNext(dest);

            } catch (Exception e) {
                emitter.onError(e);
            }
            emitter.onComplete();
        }
    })
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread());
}



/**
 * 矫正裁剪的sample位置
 *
 * @param track 视频轨道
 * @param cutHere 裁剪位置
 * @param next 是否还继续裁剪
 */
private static double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
    double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
    long currentSample = 0;
    double currentTime = 0;
    for (int i = 0; i < track.getSampleDurations().length; i++) {
        long delta = track.getSampleDurations()[i];

        if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
            // samples always start with 1 but we start with zero therefore +1(采样的下标从1开始而不是0开始,所以要+1 )
            timeOfSyncSamples[Arrays
                .binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
        }
        currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
        currentSample++;

    }
    double previous = 0;
    for (double timeOfSyncSample : timeOfSyncSamples) {
        if (timeOfSyncSample > cutHere) {
            if (next) {
                return timeOfSyncSample;
            } else {
                return previous;
            }
        }
        previous = timeOfSyncSample;
    }
    return timeOfSyncSamples[timeOfSyncSamples.length - 1];
}
    方案2:ffmeepg  待完善
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐