基于javacv的视频编码格式判断及转码

目标

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

依赖

<!-- 依赖很多,不需要的自行排除 -->
<!-- 转码功能只需要以ffmpeg、javacpp、javacv、openblas、opencv开头的jar包依赖 -->
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</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 = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
			recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
			recorder.setFormat(format);
			recorder.setFrameRate(frameGrabber.getFrameRate());
			recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
			recorder.setAspectRatio(frameGrabber.getAspectRatio());
			recorder.setAudioOptions(frameGrabber.getAudioOptions());
			recorder.setSampleRate(frameGrabber.getSampleRate());
			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);
	}
	
}

升级代码

package com.videotest;

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

public class VideoUtils {
	private static final Logger logger = LoggerFactory.getLogger(VideoUtils.class);
	
	private static final String DEFAULT_FORMAT = "mp4";
	
	/**
	 * 判断视频编码是否H264并且音频编码是否ACC
	 * 现代浏览器对视频编码为H264并且音频编码为ACC并且封装格式为mp4格式的视频支持程度最好
	 * @param absolutePath 视频文件路径
	 * @return
	 * @throws Exception
	 */
	public static boolean isH264AndACC(String absolutePath) throws Exception {
		boolean isH264AndACC = false;
		String path = absolutePath;
		FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(path);
		try {
			frameGrabber.start();
			if (frameGrabber.getVideoCodec() == avcodec.AV_CODEC_ID_H264
					&& frameGrabber.getAudioCodec() == avcodec.AV_CODEC_ID_AAC) {
				isH264AndACC = true;
			}
		} finally {
			try {
				frameGrabber.close();
			} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
				logger.warn("frameGrabber.close异常", e);
			}
		}
		return isH264AndACC;
	}
	
	/**
	 * 将视频文件转码为视频编码为H264并且音频编码为ACC的mp4格式的视频
	 * @param absolutePath 视频文件路径
	 * @return
	 * @throws Exception
	 */
	public static String convert(String absolutePath) throws Exception {
		return convert(absolutePath, DEFAULT_FORMAT);
	}
	
	/**
	 * 将视频文件转码为视频编码为H264并且音频编码为ACC的视频
	 * @param absolutePath 视频文件路径
	 * @param format 转码后的封装格式
	 * @return
	 * @throws Exception
	 */
	public static String convert(String absolutePath, String format) throws Exception {
		String path = absolutePath;
		FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(path);
		String fileName = null;
		Frame captured_frame = null;
		FFmpegFrameRecorder recorder = null;
		
		try {
			int index = path.lastIndexOf(".");
			if (index < 0) {
				index += path.length();
			}
			fileName = path.replace(path.substring(index), "_recode." + format);
			frameGrabber.start();
			recorder = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
			recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
			recorder.setFormat(format);
			recorder.setFrameRate(frameGrabber.getFrameRate());
			recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
			recorder.setAspectRatio(frameGrabber.getAspectRatio());
			recorder.setAudioBitrate(frameGrabber.getAudioBitrate());
			recorder.setAudioOptions(frameGrabber.getAudioOptions());
			recorder.setSampleRate(frameGrabber.getSampleRate());
			recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
			recorder.start();
			while (true) {
				captured_frame = frameGrabber.grabFrame();
				
				if (captured_frame == null) {
					logger.info(fileName + "转码完成");
					break;
				}
				recorder.record(captured_frame);
			}
		} finally {
			if (recorder != null) {
				try {
					recorder.close();
				} catch (Exception e) {
					logger.warn("recorder.close异常", e);
				}
			}
			try {
				frameGrabber.close();
			} catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
				logger.warn("frameGrabber.close异常", e);
			}
		}
		return fileName;
	}
	
}

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值