好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!-- 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编辑器 主方法运行。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

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 视频转音频内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_40986713/article/details/115752334

查看更多关于java仅用30行代码就实现了视频转音频的批量转换的详细内容...

  阅读:12次