欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

手机转码工具(免费转视频格式的软件)

程序员文章站 2023-11-20 22:49:22
问题:原视频的编码格式为h265,在小程序上面无法播放,需要转为mpeg-4格式。1、引入相关依赖 org.bytedeco&...

问题:原视频的编码格式为h265,在小程序上面无法播放,需要转为mpeg-4格式。

1、引入相关依赖

<dependency>
     <groupid>org.bytedeco</groupid>
    <artifactid>javacv</artifactid>
    <version>1.4.1</version>
</dependency>

<dependency>
    <groupid>org.bytedeco</groupid>
    <artifactid>javacpp</artifactid>
    <version>1.4.1</version>
</dependency>

<dependency>
    <groupid>org.bytedeco.javacpp-presets</groupid>
    <artifactid>opencv-platform</artifactid>
    <version>3.4.1-1.4.1</version>
</dependency>

<dependency>
    <groupid>org.bytedeco.javacpp-presets</groupid>
    <artifactid>ffmpeg-platform</artifactid>
    <version>3.4.2-1.4.1</version>
</dependency>

2、视频转码工具类

import org.bytedeco.javacv.frame;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.framegrabber;
import org.bytedeco.javacv.framerecorder;
import org.bytedeco.javacv.ffmpegframegrabber;
import org.bytedeco.javacv.ffmpegframerecorder;

/**
 * 视频转码工具类
 */
public class videoconvertutil {
	
    public void convert(string inputfile, string outputfile) throws exception {
        ffmpegframegrabber grabber = ffmpegframegrabber.createdefault(inputfile);
        frame captured_frame;
        ffmpegframerecorder recorder = null;

        try {
            grabber.start();

            recorder = new ffmpegframerecorder(outputfile, grabber.getimagewidth(), grabber.getimageheight(), grabber.getaudiochannels());
            recorder.setvideocodec(avcodec.av_codec_id_mpeg4);
            recorder.setformat("mp4");
            recorder.setframerate(grabber.getframerate());
            recorder.setsamplerate(grabber.getsamplerate());
            recorder.setvideobitrate(grabber.getvideobitrate());
            recorder.setaspectratio(grabber.getaspectratio());
            recorder.setaudiobitrate(grabber.getaudiobitrate());
            recorder.setaudiooptions(grabber.getaudiooptions());
            recorder.setaudiocodec(avcodec.av_codec_id_aac);
            recorder.start();

            while (true) {
                captured_frame = grabber.grabframe();
                if (captured_frame == null) {
                    system.out.println("转码完成");
                    break;
                }
                recorder.record(captured_frame);
            }

        } catch (framerecorder.exception e) {
            e.printstacktrace();
        } finally {
            if (recorder != null) {
                try {
                    recorder.close();
                } catch (exception e) {
                    system.out.println("recorder.close异常" + e);
                }
            }

            try {
                grabber.close();
            } catch (framegrabber.exception e) {
                system.out.println("framegrabber.close异常" + e);
            }
        }
    }
}

3、调用工具类方法

videoconvertutil convertutil = new videoconvertutil();
convertutil.convert(url, videosavepath);

路漫漫其修远兮,吾将上下而求索

译文:在追寻真理方面,前方的道路还很漫长,但我将百折不挠,不遗余力地去追求和探索。