iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java仅用30行代码就实现了视频转音频的批量转换
  • 105
分享到

java仅用30行代码就实现了视频转音频的批量转换

2024-04-02 19:04:59 105人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

本功能实现需要用到第三方jar包 jave,JAVE 是java调用FFmpeg的封装工具。 Spring Boot项目pom文件中添加以下依赖 <!--

本功能实现需要用到第三方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";
 
 
    
    public static String  getNewFileName(String sourceFilePath) {
        File source = new File(sourceFilePath);
        String fileName=source.getName().substring(0, source.getName().lastIndexOf("."));
        return fileName+"."+outputFormat;
    }
 
    
    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();
        }
    }
 
    
    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 视频转音频内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: java仅用30行代码就实现了视频转音频的批量转换

本文链接: https://www.lsjlt.com/news/123850.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作