乐趣区

关于java:Java获取视频文件时长

网上大多数博客的代码通过测试都是无用的,因为业务须要所以本人实现并封装了工具类供大家参考。

引入 jave(多媒体解决包)pom 依赖

<!-- jave 外围依赖 -->
<dependency>
     <groupId>ws.schild</groupId>
     <artifactId>jave-core</artifactId>
     <version>2.4.6</version>
</dependency>

<!-- 依据不同操作系统引入不同 FFmpeg 包 -->

<!-- window32 位 FFmpeg -->
<dependency>
     <groupId>ws.schild</groupId>
     <artifactId>jave-native-win32</artifactId>
     <version>2.4.6</version>
</dependency>
<!-- window64 位 FFmpeg -->
<dependency>
     <groupId>ws.schild</groupId>
     <artifactId>jave-native-win64</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>
<!-- macos64 位 FFmpeg -->
<dependency>
     <groupId>ws.schild</groupId>
     <artifactId>jave-native-osx64</artifactId>
     <version>2.4.6</version>
</dependency>

<!-- Apache Commons -->
<dependency>
     <groupId>commons-fileupload</groupId>
     <artifactId>commons-fileupload</artifactId>
     <version>1.2.1</version>
</dependency>
<dependency>
     <groupId>commons-io</groupId>
     <artifactId>commons-io</artifactId>
     <version>2.7</version>
</dependency>

获取视频文件时长工具类:

import com.google.common.collect.ImmutableList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
import java.io.File;
@Slf4j
public class FileUtil {
    private static ImmutableList<String> videoSuffixList = ImmutableList.of("mp4", "mov", "avi", "mkv", "m4v", "wmv",
 "asf", "asx", "rm", "rmvb", "3gp", "dat", "flv", "vob");
 /**
 * 获取视频时长 * @param file 视频文件
 * @return 时长(秒)*/ public static int getVideoDuration(MultipartFile file){
        int duration = 0;
 File tempFile = null;
 try{tempFile = File.createTempFile(UUIDUtil.uuid(), "." + FilenameUtils.getExtension(file.getOriginalFilename()));
 file.transferTo(tempFile);
 MultimediaObject multimediaObject = new MultimediaObject(tempFile);
 MultimediaInfo info = multimediaObject.getInfo();
 duration = (int) Math.ceil((double) info.getDuration() / 1000);
 } catch (Exception e){log.error("获取视频时长失败", e);
 } finally {if (tempFile != null){tempFile.delete();
 }
        }
        return duration;
 }
    /**
 * 判断文件是否是视频 * @param file 文件
 * @return 是否是啊视频
 */ public static boolean isVideo(MultipartFile file){return videoSuffixList.contains(FilenameUtils.getExtension(file.getOriginalFilename()));
 }
}

在下面的代码中有一个坑须要留神

MultipartFile.transferTo 会报 FileNotFound 异样。

起因:CommonsMultipartResolver 的 maxInMemorySize 默认值是 10M,所以文件大小小于 10M 不会创立到本地,所以须要设置

@Configuration
public class MultipartResolverConfig {
    @Bean
 public CommonsMultipartResolver multipartResolver(){CommonsMultipartResolver resolver = new CommonsMultipartResolver();
 resolver.setDefaultEncoding("UTF-8");
 resolver.setMaxInMemorySize(-1);
 return resolver;
 }
}
退出移动版