栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

  
    ws.schild
    jave-all-deps
    2.6.0
  
  
    ws.schild
    jave-core
    2.4.5
  

  
  
    ws.schild
    jave-native-win32
    2.4.5
  

  
  
    ws.schild
    jave-native-win64
    2.4.5
  

  
  
    ws.schild
    jave-native-linux32
    2.4.6
  

  
  
    ws.schild
    jave-native-linux64
    2.4.6
  

    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 {


@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;
}


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;
}

 
@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 视频时长、大小的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号