자바 는 30 줄 코드 만 사용 하여 비디오 오디 오 를 대량으로 변환 하 였 다.

이 기능 을 실현 하려 면 제3자 jar 패키지 jave,JAVE 를 사용 해 야 합 니 다. 자바 에서 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>
자바 단일 클래스 구현 코드 를 Spring boot 프로젝트 에 복사 하여 아이디어 편집기 메 인 방법 으로 실행 합 니 다.

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 정도 다.
자바 가 30 줄 코드 만으로 비디오 오디 오 를 대량으로 변환 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 비디오 오디 오 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기