1

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

引入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()));
 }
}
@Configuration
public class MultipartResolverConfig {
    @Bean
 public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
 resolver.setDefaultEncoding("UTF-8");
 resolver.setMaxInMemorySize(Integer.MAX_VALUE);
 return resolver;
 }
}

serein
18 声望0 粉丝