视频格式转换工具,用到,之前是用的,作者已经不维护了

时间:2025-01-31 11:07:59

视频格式转换工具,用到,之前是用的,作者已经不维护了 ,最近又看到了这个,算是它的升级版,所以自己摸索它的用法,也根据之前的方法一步步试的。

添加依赖:

核心依赖

        <groupId></groupId>
            <artifactId>jave-core</artifactId>
            <version>3.0.1</version>
        </dependency>

系统平台的依赖,看你程序使用的是什么系统,两个都可以加,这个是为了使用包里提供的工具,没有这个会出现错误!
        <dependency>
            <groupId></groupId>
            <artifactId>jave-nativebin-win64</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>jave-nativebin-linux64</artifactId>
            <version>3.0.1</version>
        </dependency>

package ;

import ;
import ;
import ;
import ;
import ;
import ;

import org.;
import org.;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 *
 * @ClassName VideoFormatUtil
 * @Description 视频格式转换工具
 * @Author geekcjj
 * @Date 2020年12月13日 上午10:52:28
 */
public class VideoFormatUtil {

	public static Logger Log = ();

	/**
	 * 视频文件转音频文件
	 * @param videoPath
	 * @param audioPath
	 * @return
	 */
	public static boolean videoToAudio(String videoPath, String audioPath) {
		File fileMp4=new File(videoPath);
		File fileMp3=new File(audioPath);

		//Audio Attributes
		AudioAttributes audio = new AudioAttributes();
		("libmp3lame");
		(128000);
		(2);
		(44100);

		//Encoding attributes
		EncodingAttributes attrs = new EncodingAttributes();
		("mp3");
		(audio);
		Encoder encoder = new Encoder();
		MultimediaObject mediaObject=new MultimediaObject(fileMp4);
		try {
			(mediaObject, fileMp3, attrs);
			("File MP4 convertito in MP3");
			return true;
		} catch (Exception e) {
			("File non convertito");
			(());
			return false;
		}
	}

	/**
	 * 获取视频的基本信息,视频长宽高,视频的大小等
	 * @param fileSource
	 * @return
	 */
	public static VideoItem getVideoInfo(String fileSource) {
		// String filePath =
		// ().getResource(fileSource).getPath();
		File source = new File(fileSource);
		//Encoder encoder = new Encoder();
		FileInputStream fis = null;
		FileChannel fc = null;
		VideoItem videoInfo = null;
		try {
			MultimediaObject MultimediaObject=new MultimediaObject(source);
			MultimediaInfo m = ();
			fis = new FileInputStream(source);
			fc = ();
			videoInfo = new VideoItem(().getSize().getWidth(), ().getSize().getHeight(), (),
					(), ());
			(videoInfo);
		} catch (Exception e) {
			();
		} finally {
			if (null != fc) {
				try {
					();
				} catch (IOException e) {
					();
				}
			}
			if (null != fis) {
				try {
					();
				} catch (IOException e) {
					();
				}
			}
		}
		return videoInfo;
	}

    /**
	 * 截取视频中某一帧作为图片
	 * @param videoPath
	 * @param imagePath
	 * @return
	 */
	public static boolean getVideoProcessImage(String videoPath,String imagePath){
		long times = ();
		File videoSource = new File(videoPath);
		File imageTarget = new File(imagePath);
		MultimediaObject object = new MultimediaObject(videoSource);
		try {
			MultimediaInfo multimediaInfo = ();
			VideoInfo videoInfo=();
			VideoAttributes video = new VideoAttributes();
			("png");
			(());
			EncodingAttributes attrs = new EncodingAttributes();
			//VideoAttributes attrs = ().get();
			("image2");
			(11f);//设置偏移位置,即开始转码位置(11秒)
			(0.01f);//设置转码持续时间(1秒)
			(video);
			Encoder encoder = new Encoder();
			(object,imageTarget,attrs);
			return true;
		} catch (EncoderException e) {
			();
			return false;
		}
	}

    /**
	 * m4r音频格式转换为mp3,audioPath可更换为要转换的音频格式
	 * @param audioPath
	 * @param mp3Path
	 */
	public static void m4rToMp3(String audioPath,String mp3Path){
		File source = new File(audioPath);
		File target = new File(mp3Path);
		AudioAttributes audio = new AudioAttributes();
		("libmp3lame");
		(new Integer(128000));
		(new Integer(2));
		(new Integer(44100));
		EncodingAttributes attrs = new EncodingAttributes();
		("mp3");
		(audio);
		Encoder encoder = new Encoder();
		try {
			(new MultimediaObject(source), target, attrs);
		} catch (EncoderException e) {
			();
		}
	}

    /**
	 * 从和视频中提取音频wav
	 * @param aviPath
	 * @param targetWavPath
	 */
	public static void videoExtractAudio(String aviPath,String targetWavPath){
		File source = new File(aviPath);
		File target = new File(targetWavPath);
		AudioAttributes audio = new AudioAttributes();
		("pcm_s16le");
		EncodingAttributes attrs = new EncodingAttributes();
		("wav");
		(audio);
		Encoder encoder = new Encoder();
		try {
			(new MultimediaObject(source), target, attrs);
		} catch (EncoderException e) {
			();
		}
	}

	/**
	 * 视频转换为手机可播放的格式
	 * @param sourceVideo 
	 * @param targetVideo targetVideo.3gp
	 */
	public static void videoToMobileVideo(String sourceVideo, String targetVideo){
		File source = new File("");
		File target = new File("target.3gp");
		AudioAttributes audio = new AudioAttributes();
		("libfaac");
		(new Integer(128000));
		(new Integer(44100));
		(new Integer(2));
		VideoAttributes video = new VideoAttributes();
		("mpeg4");
		(new Integer(160000));
		(new Integer(15));
		(new VideoSize(176, 144));
		EncodingAttributes attrs = new EncodingAttributes();
		("3gp");
		(audio);
		(video);
		Encoder encoder = new Encoder();
		try {
			(new MultimediaObject(source), target, attrs);
		} catch (EncoderException e) {
			();
		}
	}

	public static void main(String[] args) {
		//("数据 = [" + getVideoProcessImage("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4","E:/MyFile/") + "]");
		//("数据 = [" + getVideoInfo("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4") + "]");
		//("数据 = [" + videoToAudio("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4","E:/MyFile/sfaafasddxgd.mp3") + "]");
	}

}