java video transcoding video compression

Video related concepts

Video ( Video ) generally refers to various technologies that capture, record, process, store, transmit and reproduce a series of still images in the form of electrical signals . When continuous image changes exceed 24 frames per second , according to the principle of persistence of vision , the human eye cannot distinguish a single static image; it looks like a smooth and continuous visual effect, such a continuous image is called a video.

The container contains: audio, subtitles, pictures

Video container formats: 3GP, AVI, FLV, MP4, M3U8, MPG, ASF, WMV, MKV, MOV, TS, WebM, MXF.

Video encoding formats: H.264/AVC, H.263, H.263+, H.265, MPEG-1, MPEG-2, MPEG-4, MJPEG, VP8, VP9, ​​Quicktime, RealVideo, Windows Media Video.

Audio encoding formats: AAC, AC-3, ADPCM, AMR, DSD, MP1, MP2, MP3, PCM, RealAudio, Windows Media Audio.

How to see the video encoding format:

Therefore, video transcoding is the encoding format, and video compression is the adjustment of video resolution, bit rate, and frame rate (including audio)

Resolution: width*height of the video, commonly used are 720×480 (480p), 1280×720 (720p), 1920×1080

Video Size Resolution Suggested Bit Rate

Code rate formula 

After the above concepts have been analyzed, the following code

pom file to add dependencies:

 <!--视频转码依赖-->
        <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>

Transcoding compression demo:

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacv.*;

/**
 * @ClassName :VideoConvertUtil
 * @Description :
 */
public class VideoConvertUtil {
    public static void convert(String inputFile, String outputFile) throws Exception {
        System.out.println("转码开始");
        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(inputFile);
        Frame captured_frame;
        FFmpegFrameRecorder recorder = null;

        try {
            grabber.start();
            int videoCodec = grabber.getVideoCodec();
            System.out.println("videoCodec:"+videoCodec);
            /**
             * ImageWidth:视频宽
             * ImageHeight:视频高
             * audioChannels:设置重新编码的音频流中使用的声道数(1 =单声道,2 = 双声道(立体声))。如果未设置任何声道值,则编码器将选择默认值 0。
             */
            recorder = new FFmpegFrameRecorder(outputFile, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
//            recorder.setImageHeight(640);
//            recorder.setImageWidth(480);
            //recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
            recorder.setFormat("mp4");
            // 视频帧率(保证视频质量的情况下最低25,低于25会出现闪屏)
            recorder.setFrameRate(grabber.getFrameRate());
            recorder.setSampleRate(grabber.getSampleRate());
            //视频编码属性配置 H.264 H.265 MPEG
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            //设置视频比特率,单位:b
            recorder.setVideoBitrate(grabber.getVideoBitrate());
            recorder.setAspectRatio(grabber.getAspectRatio());
            // yuv420p,像素
            recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
            //设置视频质量
//            recorder.setVideoQuality(avutil.FF_LAMBDA_MAX);
            recorder.setVideoQuality(avutil.FF_LAMBDA_SHIFT);
            // 设置音频通用编码格式
            recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
            //设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 128000 = 182kb)
            recorder.setAudioBitrate(grabber.getAudioBitrate());
            recorder.setAudioOptions(grabber.getAudioOptions());
            recorder.start();

            while (true) {
                captured_frame = grabber.grabFrame();
                System.out.println("转码循环---"+System.currentTimeMillis());
                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);
            }
        }
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
//        String url = "/Users/Downloads/视频/视频 (1)/最新-合片剪辑的3分17.mp4";
//        String url = "/Users/Downloads/视频/健康资讯视频.mp4";
        String url = "/Users/Downloads/132451525666042.mp4";
        String videoSavePath = "/Users/Downloads/视频/test_FF_LAMBDA_SHIFT.mp4";
        try {
            VideoConvertUtil.convert(url, videoSavePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime - startTime));
    }

}

The above is all about Java transcoding and compression.

Guess you like

Origin blog.csdn.net/gao_yuwushengchu/article/details/123893769