欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java获取视频时长、大小的示例

程序员文章站 2022-04-06 23:05:21
项目中有这样一个需求,网页上上传了一个视频,需要获取此视频的时长、大小,把这两个数据返回给前台在页面显示。后台使用的是springboot框架,项目部署在linux上面。下面是核心代码:1、pom文件...

    项目中有这样一个需求,网页上上传了一个视频,需要获取此视频的时长、大小,把这两个数据返回给前台在页面显示。后台使用的是springboot框架,项目部署在linux上面。下面是核心代码:

    1、pom文件中需要导入的jar包依赖

    (分为两部分:核心包、ffmpeg包两部分,ffmpeg包又分为windows环境以及linux环境,同时又区分32位系统以及64位系统。针对于不同的运行环境要导入不同的包,这一点对于开发、测试环境为window而生产环境为linux的情况,尤其要注意需要导入所有的包,使其在两种环境下都能够使用)

  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-all-deps</artifactid>
    <version>2.6.0</version>
  </dependency>
  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-core</artifactid>
    <version>2.4.5</version>
  </dependency>

  <!-- window32位 ffmpeg -->
  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-native-win32</artifactid>
    <version>2.4.5</version>
  </dependency>

  <!-- window64位 ffmpeg -->
  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-native-win64</artifactid>
    <version>2.4.5</version>
  </dependency>

  <!-- linux32位 ffmpeg -->
  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-native-linux32</artifactid>
    <version>2.4.6</version>
  </dependency>

  <!-- linux64位 ffmpeg -->
  <dependency>
    <groupid>ws.schild</groupid>
    <artifactid>jave-native-linux64</artifactid>
    <version>2.4.6</version>
  </dependency>

    2、controller层代码

package com.aaa.bbb.controller;

import io.swagger.annotations.api;
import lombok.extern.slf4j.slf4j;
import org.apache.commons.io.fileutils;
import org.springframework.util.resourceutils;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;
import ws.schild.jave.multimediainfo;
import ws.schild.jave.multimediaobject;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.math.bigdecimal;
import java.math.roundingmode;
import java.nio.channels.filechannel;

@slf4j
@restcontroller
@requestmapping("/readvideo")
@api(tags = "获取视频时长、大小相关信息的接口")
public class readvideocontroller {

/**
 * 获取视频时长
 *
 * @param fileurl
 * @return
 */
@postmapping("/videolengthandsize")
public static string getlengthandsize(@requestparam string fileurl) throws filenotfoundexception {
  readvideocontroller r = new readvideocontroller();
  string path = resourceutils.geturl("classpath:").getpath() + "static";
  system.out.println("666666666666666666666666666666【" + path + "】666666666666666666666666666666】");
  fileurl = path + fileurl;
  string videolength = r.readvideotime(fileurl);//视频时长

  system.out.println("===========================视频时长:" + videolength + "===========================");
  return videolength;
}

/**
 * 视频时长
 *
 * @param fileurl
 * @return
 */
public static string readvideotime(string fileurl) {
  file source = new file(fileurl);
  string length = "";
  try {
    multimediaobject instance = new multimediaobject(source);
    multimediainfo result = instance.getinfo();
    long ls = result.getduration() / 1000;
    integer hour = (int) (ls / 3600);
    integer minute = (int) (ls % 3600) / 60;
    integer second = (int) (ls - hour * 3600 - minute * 60);
    string hr = hour.tostring();
    string mi = minute.tostring();
    string se = second.tostring();
    if (hr.length() < 2) {
      hr = "0" + hr;
    }
    if (mi.length() < 2) {
      mi = "0" + mi;
    }
    if (se.length() < 2) {
      se = "0" + se;
    }
    length = hr + ":" + mi + ":" + se;
  } catch (exception e) {
    e.printstacktrace();
  }
  return length;
}

 /**
 * 视频大小
 *
 * @param source
 * @return
 */
@suppresswarnings({"resource"})
public static string readvideosize(file source) {
  filechannel fc = null;
  string size = "";
  try {
    fileinputstream fis = new fileinputstream(source);
    fc = fis.getchannel();
    bigdecimal filesize = new bigdecimal(fc.size());
    size = filesize.divide(new bigdecimal(1024 * 1024), 2, roundingmode.half_up) + "mb";
  } catch (filenotfoundexception e) {
    e.printstacktrace();
  } catch (ioexception e) {
    e.printstacktrace();
  } finally {
    if (null != fc) {
      try {
        fc.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }
  return size;
}
}

以上就是java获取视频时长、大小的示例的详细内容,更多关于java 视频时长、大小的资料请关注其它相关文章!