Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java to realize batch conversion from Video to Audio

Shulou Source: shulou.com Published: 2022-06-01 11:47:48 10月02日 Update

This article mainly introduces how to use Java to achieve video to audio batch conversion, the article is very detailed, has a certain reference value, interested friends must read it!

The implementation of this function requires the use of a third-party jar package jave,JAVE is an encapsulation tool for java to call FFmpeg.

Add the following dependencies to the spring boot project pom file

Ws.schild jave-core 3.1.1 ws.schild jave-nativebin-win64 3.1.1 Ws.schild jave-nativebin-linux64 3.1.1

Java single-class implementation code, copied to the Spring boot project, using the idea editor main method to run.

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 {/ / Audio format private static String outputFormat= "mp3" to be output / * obtain the converted file name * @ param sourceFilePath: source video file path * @ return * / public static String getNewFileName (String sourceFilePath) {File source = new File (sourceFilePath); String fileName=source.getName () .substring (0, source.getName () .lastIndexOf (".")); return fileName+ "." + outputFormat } / * convert audio format * @ param sourceFilePath: source video file path * @ param targetFilePath: destination music file path * @ return * / public static void transform (String sourceFilePath, String targetFilePath) {File source = new File (sourceFilePath); File target = new File (targetFilePath); / / set audio attribute AudioAttributes audio = new AudioAttributes () Audio.setCodec (null); / / set transcoding properties EncodingAttributes attrs = new EncodingAttributes (); attrs.setOutputFormat (outputFormat); attrs.setAudioAttributes (audio); try {/ / Audio conversion format class Encoder encoder = new Encoder (); MultimediaObject mediaObject=new MultimediaObject (source); encoder.encode (mediaObject, target, attrs) System.out.println ("conversion completed...");} catch (EncoderException e) {e.printStackTrace () }} / * batch convert audio format * @ param sourceFolderPath: source video folder path * @ param targetFolderPath: destination music folder path * @ return * / public static void batchTransform (String sourceFolderPath, String targetFolderPath) {File sourceFolder = new File (sourceFolderPath) If (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") }} above are all the contents of the article "how to use Java to convert video to audio in batch". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Audio file video path format content properties folder target article music project code value interest function buddy partner tool file name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Shulou Tech Info vpn MariaDB