最完整获取视频时长以及将MultipartFile转化成File的工具类(原创)

最完整获取视频时长以及将MultipartFile转化成File的工具类(原创)

需要引入jar包 下载地址

http://www.sauronsoftware.it/projects/jave/download.php

maven首先需要手动引入外部的包

    <dependency>
		<groupId>jave</groupId>
		<artifactId>jave</artifactId>
		<version>1.0.2</version>
		<scope>system</scope>
		<systemPath>${basedir}/lib/jave-1.0.2.jar</systemPath>
	</dependency>

<build>
		<plugins>
		    <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.10</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>compile</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
							<includeScope>system</includeScope>
						</configuration>
					</execution>
				</executions>
	   </plugin>
   </plugins>
</build>

//这是一个外部的包,在根目录下创建lib文件夹,将包放进去

 import it.sauronsoftware.jave.Encoder;
    
    import it.sauronsoftware.jave.MultimediaInfo;
    
    import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
    
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    
    import java.io.IOException;
    
    /**
 * 用于处理视频时间/大小
 */
public class VideoUtil {

    /**
     * 获取视频时长(时分秒)
     *
     * @param ms
     * @return
     */
    public static String ReadVideoTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
        if (strHour.equals("00")) {
            return strMinute + ":" + strSecond;
        } else {
            return strHour + ":" + strMinute + ":" + strSecond;
        }
    }

    /**
     * 获取视频时长(毫秒)
     *
     * @param file
     * @return
     */
    public static String ReadVideoTimeMs(MultipartFile file) {
        Encoder encoder = new Encoder();
        String length = "";
        try {
            // 获取文件类型
            String fileName = file.getContentType();
            // 获取文件后缀
            String pref = fileName.indexOf("/") != -1 ? fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()) : null;
            String prefix = "." + pref;
            // 用uuid作为文件名,防止生成的临时文件重复
            final File excelFile = File.createTempFile(UUIDUtil.getUUID(), prefix);
            // MultipartFile to File
            file.transferTo(excelFile);
            MultimediaInfo m = encoder.getInfo(excelFile);
            long ls = m.getDuration();
            length = String.valueOf(ls);
            //程序结束时,删除临时文件
            VideoUtil.deleteFile(excelFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

    /**
     * 删除
     *
     * @param files
     */
    private static void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}

如有侵权,联系我

发布了5 篇原创文章 · 获赞 1 · 访问量 1634
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览