用Java编码视频
我正在尝试创建一个屏幕记录器类型的东西,但我不知道如何将多个BuffereImage编码为mp4这样的可监视格式。我更喜欢纯Java代码,我不能使用最终用户在工作前必须安装的任何东西。我可以得到所有的音频和帧,我只需要找出如何制作视频 如何用Java对视频和音频进行编码?用Java编码视频,java,video,video-encoding,mp4,Java,Video,Video Encoding,Mp4,我正在尝试创建一个屏幕记录器类型的东西,但我不知道如何将多个BuffereImage编码为mp4这样的可监视格式。我更喜欢纯Java代码,我不能使用最终用户在工作前必须安装的任何东西。我可以得到所有的音频和帧,我只需要找出如何制作视频 如何用Java对视频和音频进行编码? JpegImagesToMovie(使用核心JMF-无本地版本)将JPEG图像组转换为Quicktime.MOV JMF也有应用程序。用于合并声音和音频 您可以使用JCodec()提供的开源纯java H.264编解码器。要将
- JpegImagesToMovie(使用核心JMF-无本地版本)将JPEG图像组转换为Quicktime.MOV
- JMF也有应用程序。用于合并声音和音频
public class SequenceEncoder {
private SeekableByteChannel ch;
private Picture toEncode;
private RgbToYuv420 transform;
private H264Encoder encoder;
private ArrayList<ByteBuffer> spsList;
private ArrayList<ByteBuffer> ppsList;
private CompressedTrack outTrack;
private ByteBuffer _out;
private int frameNo;
private MP4Muxer muxer;
public SequenceEncoder(File out) throws IOException {
this.ch = NIOUtils.writableFileChannel(out);
// Transform to convert between RGB and YUV
transform = new RgbToYuv420(0, 0);
// Muxer that will store the encoded frames
muxer = new MP4Muxer(ch, Brand.MP4);
// Add video track to muxer
outTrack = muxer.addTrackForCompressed(TrackType.VIDEO, 25);
// Allocate a buffer big enough to hold output frames
_out = ByteBuffer.allocate(1920 * 1080 * 6);
// Create an instance of encoder
encoder = new H264Encoder();
// Encoder extra data ( SPS, PPS ) to be stored in a special place of
// MP4
spsList = new ArrayList<ByteBuffer>();
ppsList = new ArrayList<ByteBuffer>();
}
public void encodeImage(BufferedImage bi) throws IOException {
if (toEncode == null) {
toEncode = Picture.create(bi.getWidth(), bi.getHeight(), ColorSpace.YUV420);
}
// Perform conversion
for (int i = 0; i < 3; i++)
Arrays.fill(toEncode.getData()[i], 0);
transform.transform(AWTUtil.fromBufferedImage(bi), toEncode);
// Encode image into H.264 frame, the result is stored in '_out' buffer
_out.clear();
ByteBuffer result = encoder.encodeFrame(_out, toEncode);
// Based on the frame above form correct MP4 packet
spsList.clear();
ppsList.clear();
H264Utils.encodeMOVPacket(result, spsList, ppsList);
// Add packet to video track
outTrack.addFrame(new MP4Packet(result, frameNo, 25, 1, frameNo, true, null, frameNo, 0));
frameNo++;
}
public void finish() throws IOException {
// Push saved SPS/PPS to a special storage in MP4
outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList));
// Write MP4 header and finalize recording
muxer.writeHeader();
NIOUtils.closeQuietly(ch);
}
}
公共类SequenceEncoder{
私人频道;
私人图片编码;
私有RgbToYuv420变换;
专用H264编码器;
私有数组列表;
私有数组列表;
私人压缩跑道出口;
二等兵拜特伯弗(ByteBuffer)出局;;
私有int框架编号;
私人MP4Muxer-muxer;
公共SequenceEncoder(文件输出)引发IOException{
this.ch=NIOUtils.writableFileChannel(out);
//转换以在RGB和YUV之间转换
变换=新的RgbToYuv420(0,0);
//存储编码帧的多路复用器
muxer=新的MP4Muxer(ch,品牌.MP4);
//将视频曲目添加到muxer
outTrack=muxer.addTrackForCompressed(TrackType.VIDEO,25);
//分配一个足以容纳输出帧的缓冲区
_out=字节缓冲分配(1920*1080*6);
//创建编码器的实例
编码器=新的H264编码器();
//编码器额外数据(SPS、PPS)存储在
//MP4
spsList=newarraylist();
ppsList=newarraylist();
}
public void encodeImage(BufferedImage bi)引发IOException{
如果(toEncode==null){
toEncode=Picture.create(bi.getWidth()、bi.getHeight()、ColorSpace.YUV420);
}
//执行转换
对于(int i=0;i<3;i++)
fill(toEncode.getData()[i],0);
transform.transform(AWTUtil.fromBuffereImage(bi),toEncode);
//将图像编码到H.264帧中,结果存储在“\u out”缓冲区中
_out.clear();
ByteBuffer结果=编码器.encodeFrame(_out,toEncode);
//基于上面的帧形成正确的MP4数据包
spsList.clear();
ppsList.clear();
H264Utils.encodeMOVPacket(结果、spsList、PPList);
//将数据包添加到视频曲目
addFrame(新的MP4Package(结果,frameNo,25,1,frameNo,true,null,frameNo,0));
frameNo++;
}
public void finish()引发IOException{
//将保存的SP/PP推送到MP4中的特殊存储器
outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList,ppsList));
//写入MP4标题并完成录制
muxer.writeHeader();
NIOUtils.closes(ch);
}
}
然后你可以这样使用它:
public static void main(String[] args) throws IOException {
SequenceEncoder encoder = new SequenceEncoder(new File("video.mp4"));
for (int i = 1; i < 100; i++) {
BufferedImage bi = ImageIO.read(new File(String.format("img%08d.png", i)));
encoder.encodeImage(bi);
}
encoder.finish();
}
publicstaticvoidmain(字符串[]args)引发IOException{
SequenceEncoder=新的SequenceEncoder(新文件(“video.mp4”);
对于(int i=1;i<100;i++){
BuffereImage bi=ImageIO.read(新文件(String.format(“img%08d.png”,i));
编码器。编码图像(bi);
}
编码器。完成();
}
不幸的是,JCodec发生了重大变化,因此您的示例不再编译。你能编辑你的帖子并使其与当前版本兼容吗?我也需要当前版本,有没有教程可以发给我?这似乎是当前的做法: