基于javacv的视频转码

目录

  • 目标
  • 依赖
  • 从网上找的代码
  • 遇到问题
  • 眼前的希望
  • 最终代码

目标

将所有格式的视频转码为mp4格式

依赖

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv</artifactId>
    <version>1.5.3</version>
</dependency>

从网上找的代码

咱没做过音视频开发,咱啥也不懂,只能从网上找代码,稍微加工一下

import java.io.File;

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;

public class VideoUtils {
	
	public static String convert(File file) {
		FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath());
		String fileName = null;

		Frame captured_frame = null;

		FFmpegFrameRecorder recorder = null;
		
		try {
			frameGrabber.start();
			fileName = file.getAbsolutePath().replace(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")), "_recode.mp4");
			recorder = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
			recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
			recorder.setFormat("mp4");
			recorder.setFrameRate(frameGrabber.getFrameRate());
			recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
			recorder.setAudioBitrate(192000);
			recorder.setAudioOptions(frameGrabber.getAudioOptions());
			recorder.setAudioQuality(0);
			recorder.setSampleRate(44100);
			recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
			recorder.start();
			while (true) {
				try {
					captured_frame = frameGrabber.grabFrame();
					
					if (captured_frame == null) {
						System.out.println("!!! Failed cvQueryFrame");
						break;
					}
					recorder.setTimestamp(frameGrabber.getTimestamp());
					recorder.record(captured_frame);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			recorder.stop();
			recorder.release();
			frameGrabber.stop();
			frameGrabber.release();
			recorder.close();
			frameGrabber.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		file.delete();
		return fileName;
	}

	public static void main(String[] args) throws java.lang.Exception {
		String filePath = "D:/1590473414299_20200526.3gp";
		System.out.println("开始。。。");
        String convert = convert(new File(filePath));
        System.out.println(convert);
	}
	
}

遇到问题

以上代码转码大多数视频是正常的,但转码个别的视频报了以下错误

org.bytedeco.javacv.FrameRecorder$Exception: av_interleaved_write_frame() error -22 while writing interleaved video packet.
	at org.bytedeco.javacv.FFmpegFrameRecorder.writePacket(FFmpegFrameRecorder.java:1253)
	at org.bytedeco.javacv.FFmpegFrameRecorder.recordImage(FFmpegFrameRecorder.java:1048)
	at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:935)
	at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:928)
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[amrnb @ 000000001b0d4780] Multiple frames in a packet.
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[mp4 @ 000000001d0f4040] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 320937 >= 320937
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[mp4 @ 000000001d0f4040] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1604685 >= 1604685
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[mp4 @ 000000001d0f4040] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 4814055 >= 4814055
[libx264 @ 000000001b0d1980] non-strictly-monotonic PTS
[mp4 @ 000000001d0f4040] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 5562908 >= 5562908
[mp4 @ 000000001d0f4040] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 8665299 >= 8665299
[aac @ 000000001d1004c0] 2 frames left in the queue on closing

这也太恶心了,开始从网上找解决方案,
有说是视频存在角度旋转的问题,但这例视频角度一直为0,
有说是dts和pts的问题,从异常信息中看应该是dts的问题,可是网上的解决方案都是C语言,咱也看不懂呀,就算看懂了,又能怎样,咱也不会用C语言呀。
只能尝试用java代码解决,javacv也没什么文档,网上的参考代码也只有那么一点。
看着网上的代码,改了一遍又改一遍,彻底恶心了,没有找到可解决问题的方案。

盯着屏幕上这几行代码

while (true) {
	try {
		captured_frame = frameGrabber.grabFrame();
		
		if (captured_frame == null) {
			System.out.println("!!! Failed cvQueryFrame");
			break;
		}
		recorder.setTimestamp(frameGrabber.getTimestamp());
		recorder.record(captured_frame);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

实在无从下手呀

眼前的希望

除了异常大法,还有注释大法,哪里报错注哪里,so easy!!
于是我默默注释了一行代码

while (true) {
	try {
		captured_frame = frameGrabber.grabFrame();
		
		if (captured_frame == null) {
			System.out.println("!!! Failed cvQueryFrame");
			break;
		}
//		recorder.setTimestamp(frameGrabber.getTimestamp());
		recorder.record(captured_frame);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

再次尝试,哈哈哈,竟然没报错,播放转码后的视频,也正常了。
不知道这个Timestamp是干什么用的,反正不用它就正常了。
更多格式的视频还没测试,先这样吧,附上最终代码

最终代码

import java.io.File;

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;

public class VideoUtils {
	
	public static String convert(File file) {
		FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath());
		String fileName = null;

		Frame captured_frame = null;

		FFmpegFrameRecorder recorder = null;
		
		try {
			frameGrabber.start();
			fileName = file.getAbsolutePath().replace(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")), "_recode.mp4");
			recorder = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
			recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
			recorder.setFormat("mp4");
			recorder.setFrameRate(frameGrabber.getFrameRate());
			recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
			recorder.setAudioBitrate(192000);
			recorder.setAudioOptions(frameGrabber.getAudioOptions());
			recorder.setAudioQuality(0);
			recorder.setSampleRate(44100);
			recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
			recorder.start();
			while (true) {
				try {
					captured_frame = frameGrabber.grabFrame();
					
					if (captured_frame == null) {
						System.out.println("!!! Failed cvQueryFrame");
						break;
					}
					recorder.record(captured_frame);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			recorder.stop();
			recorder.release();
			frameGrabber.stop();
			frameGrabber.release();
			recorder.close();
			frameGrabber.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		file.delete();
		return fileName;
	}

	public static void main(String[] args) throws java.lang.Exception {
		String filePath = "D:/1590473414299_20200526.3gp";
		System.out.println("开始。。。");
        String convert = convert(new File(filePath));
        System.out.println(convert);
	}
	
}

你可能感兴趣的:(java)