本功能实现需要用到第三方jar包 jave,jave 是java调用ffmpeg的封装工具。

spring boot项目pom文件中添加以下依赖

    <!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
		<dependency>
			<groupid>ws.schild</groupid>
			<artifactid>jave-core</artifactid>
			<version>3.1.1</version>
		</dependency>
     <!-- 以下依赖根据系统二选一 -->
     <!-- win系统平台的依赖 -->
		<dependency>
			<groupid>ws.schild</groupid>
			<artifactid>jave-nativebin-win64</artifactid>
			<version>3.1.1</version>
		</dependency>
     <!-- linux系统平台的依赖 -->
		<dependency>
			<groupid>ws.schild</groupid>
			<artifactid>jave-nativebin-linux64</artifactid>
			<version>3.1.1</version>
		</dependency>

java单类实现代码,复制到spring boot项目中,用idea编辑器 主方法运行。

import ws.schild.jave.encoder;
import ws.schild.jave.encoderexception;
import ws.schild.jave.multimediaobject;
import ws.schild.jave.encode.audioattributes;
import ws.schild.jave.encode.encodingattributes;
 
import java.io.file;
import java.util.arrays;
 
public class videotoaudio {
 
 
    //要输出的音频格式
    private static string outputformat="mp3";
 
 
    /**
     * 获得转化后的文件名
     * @param sourcefilepath : 源视频文件路径
     * @return
     */
    public static string  getnewfilename(string sourcefilepath) {
        file source = new file(sourcefilepath);
        string filename=source.getname().substring(0, source.getname().lastindexof("."));
        return filename+"."+outputformat;
    }
 
    /**
     * 转化音频格式
     * @param sourcefilepath : 源视频文件路径
     * @param targetfilepath : 目标音乐文件路径
     * @return
     */
    public static void transform(string sourcefilepath, string targetfilepath) {
        file source = new file(sourcefilepath);
        file target = new file(targetfilepath);
        // 设置音频属性
        audioattributes audio = new audioattributes();
        audio.setcodec(null);
        // 设置转码属性
        encodingattributes attrs = new encodingattributes();
        attrs.setoutputformat(outputformat);
        attrs.setaudioattributes(audio);
        try {
            // 音频转换格式类
            encoder encoder = new encoder();
            multimediaobject mediaobject=new multimediaobject(source);
            encoder.encode(mediaobject, target, attrs);
            system.out.println("转换已完成...");
        }  catch (encoderexception e) {
            e.printstacktrace();
        }
    }
 
    /**
     * 批量转化音频格式
     * @param sourcefolderpath : 源视频文件夹路径
     * @param targetfolderpath : 目标音乐文件夹路径
     * @return
     */
    public static void batchtransform(string sourcefolderpath, string targetfolderpath) {
        file sourcefolder = new file(sourcefolderpath);
        if(sourcefolder.list().length!=0){
            arrays.aslist(sourcefolder.list()).foreach(e->{
              transform(sourcefolderpath+"\\"+e, targetfolderpath+"\\"+getnewfilename(e));
            });
        }
    }
 
    public static void main(string[] args) {
        batchtransform("c:\\users\\tarzan\\desktop\\video","c:\\users\\tarzan\\desktop\\audio");
    }
 
}

运行结果截图

测试结果

视频格式为mp4,大小约6.65mb,转为音频格式mp3,大小约1.60mb,转化时间1s左右。

到此这篇关于java仅用30行代码就实现了视频转音频的批量转换的文章就介绍到这了,更多相关java 视频转音频内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!