android4.2 图库修剪视频后,详细信息中不显示时长

原因:

视频修改完成后没有获取剪辑视频时间,并插入数据库

TrimVideo.java

 public void run() {
                        try {
                      double trimVideoDuration = TrimVideoUtils.startTrim(mSrcFile, mDstFile, mTrimStartTime, mTrimEndTime);
                    // Update the database for adding a new video file.
                    insertContent(mDstFile, (int)(trimVideoDuration * 1000));
                              } catch (IOException e) {

.................

 

private Uri insertContent(File file, int duration) {

        long nowInMs = System.currentTimeMillis();
        long nowInSec = nowInMs / 1000;
        final ContentValues values = new ContentValues(13);
        values.put(Video.Media.TITLE, mSaveFileName);
        values.put(Video.Media.DISPLAY_NAME, file.getName());
        values.put(Video.Media.MIME_TYPE, "video/mp4");
        values.put(Video.Media.DATE_TAKEN, nowInMs);
        values.put(Video.Media.DATE_MODIFIED, nowInSec);
        values.put(Video.Media.DATE_ADDED, nowInSec);
        values.put(Video.Media.DATA, file.getAbsolutePath());
        values.put(Video.Media.SIZE, file.length());
        values.put(Video.Media.DURATION, duration);        

     // Copy the data taken and location info from src.

 

TrimVideoUtils.java

 public static double startTrim(File src, File dst, int startMs, int endMs) throws IOException

....

        double TrimVideoStartTime = Double.MAX_VALUE;
        double TrimVideoEndTime = 0;

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

.......

               if (currentTime <= startTime) {
                        // current sample is still before the new starttime
                        startSample = currentSample;
                        trackStartTime = currentTime;
                    }
                    if (currentTime <= endTime) {
                        // current sample is after the new start time and still before the new endtime
                        endSample = currentSample;
                       
trackEndTime = currentTime;

                     } else {
                        // current sample is after the end of the cropped video
                        break;
                    }
                    currentTime += (double) entry.getDelta() / (double) track.getTrackMetaData().getTimescale();
                    currentSample++;
                }
            }
            TrimVideoStartTime = Math.min(TrimVideoStartTime, trackStartTime);
            TrimVideoEndTime = Math.max(TrimVideoEndTime,trackEndTime);
            movie.addTrack(new CroppedTrack(track, startSample, endSample));
        }
        IsoFile out = new DefaultMp4Builder().build(movie);

        if (!dst.exists()) {
            dst.createNewFile();
          }

        FileOutputStream fos = new FileOutputStream(dst);
        FileChannel fc = fos.getChannel();
        out.getBox(fc);  // This one build up the memory.

        fc.close();
        fos.close();
        randomAccessFile.close();
        return TrimVideoEndTime - TrimVideoStartTime;
    }


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值