Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JCodec将一系列图像转换为视频?_Java_Video_Mp4_Encoder_Jcodec - Fatal编程技术网

Java 如何使用JCodec将一系列图像转换为视频?

Java 如何使用JCodec将一系列图像转换为视频?,java,video,mp4,encoder,jcodec,Java,Video,Mp4,Encoder,Jcodec,我正在尝试使用JCodec将一系列图像转换为JavaSE桌面应用程序中的视频。我尝试过的几种方法都产生了Windows Media Player无法播放的视频 我不清楚这是否是编解码器问题(值得怀疑),或者我没有正确创建视频。当我尝试在Windows Media Player中播放视频时,我得到: Windows Media Player无法播放该文件。玩家可能不会 支持该文件类型,或者可能不支持用于 压缩文件 这是我最近使用的拼凑样本。我真的不理解视频格式的内部结构,所以我甚至不能完全确定一些

我正在尝试使用JCodec将一系列图像转换为JavaSE桌面应用程序中的视频。我尝试过的几种方法都产生了Windows Media Player无法播放的视频

我不清楚这是否是编解码器问题(值得怀疑),或者我没有正确创建视频。当我尝试在Windows Media Player中播放视频时,我得到:

Windows Media Player无法播放该文件。玩家可能不会 支持该文件类型,或者可能不支持用于 压缩文件

这是我最近使用的拼凑样本。我真的不理解视频格式的内部结构,所以我甚至不能完全确定一些代码在做什么

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;

import javax.imageio.ImageIO;

import org.jcodec.codecs.h264.H264Encoder;
import org.jcodec.codecs.h264.H264Utils;
import org.jcodec.common.NIOUtils;
import org.jcodec.common.SeekableByteChannel;
import org.jcodec.common.model.ColorSpace;
import org.jcodec.common.model.Picture;
import org.jcodec.containers.mp4.Brand;
import org.jcodec.containers.mp4.MP4Packet;
import org.jcodec.containers.mp4.TrackType;
import org.jcodec.containers.mp4.muxer.FramesMP4MuxerTrack;
import org.jcodec.containers.mp4.muxer.MP4Muxer;
import org.jcodec.scale.AWTUtil;
import org.jcodec.scale.RgbToYuv420;

public class CreateVideo {
    private SeekableByteChannel ch;
    private Picture toEncode;
    private RgbToYuv420 transform;
    private H264Encoder encoder;
    private ArrayList<ByteBuffer> spsList;
    private ArrayList<ByteBuffer> ppsList;
    private FramesMP4MuxerTrack outTrack;
    private ByteBuffer _out;
    private int frameNo;
    private MP4Muxer muxer;

    public CreateVideo(File out) throws IOException {
        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);
    }

    public static void main(String[] args) throws IOException {
        CreateVideo encoder = new CreateVideo(new File("C:\\video\\video.mp4"));
        for (int i = 10; i < 34; i++) {
            String filename = String.format("C:\\video\\%02d.png", i);
            BufferedImage bi = ImageIO.read(new File(filename));
            encoder.encodeImage(bi);
        }
        encoder.finish();
    }
}
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.nio.ByteBuffer;
导入java.util.ArrayList;
导入java.util.array;
导入javax.imageio.imageio;
导入org.jcodec.codecs.h264.h264编码器;
导入org.jcodec.codecs.h264.H264Utils;
导入org.jcodec.common.NIOUtils;
导入org.jcodec.common.seekablebytechnel;
导入org.jcodec.common.model.ColorSpace;
导入org.jcodec.common.model.Picture;
导入org.jcodec.containers.mp4.Brand;
导入org.jcodec.containers.mp4.mp4包;
导入org.jcodec.containers.mp4.TrackType;
导入org.jcodec.containers.mp4.muxer.FramesMP4MuxerTrack;
导入org.jcodec.containers.mp4.muxer.MP4Muxer;
导入org.jcodec.scale.AWTUtil;
导入org.jcodec.scale.RgbToYuv420;
公共类CreateVideo{
私人频道;
私人图片编码;
私有RgbToYuv420变换;
专用H264编码器;
私有数组列表;
私有数组列表;
专用框架4MuxerTrack outTrack;
二等兵拜特伯弗(ByteBuffer)出局;;
私有int框架编号;
私人MP4Muxer-muxer;
公共CreateVideo(文件输出)引发IOException{
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);
}
公共静态void main(字符串[]args)引发IOException{
CreateVideo编码器=新建CreateVideo(新文件(“C:\\video\\video.mp4”);
对于(int i=10;i<34;i++){
字符串文件名=String.format(“C:\\video\\%02d.png”,i);
BuffereImage bi=ImageIO.read(新文件(文件名));
编码器。编码图像(bi);
}
编码器。完成();
}
}

如果有更简单的编解码器/容器,我就不必使用H264或MP4。唯一的要求是它应该在没有安装其他软件的情况下在基准Windows 7设备上播放。

我对jcodec的RgbToYuv420()转换有很多问题。
我使用自己的函数将RGB BuffereImage转换为Yuv420。
我遇到的一个问题是,RgbToYuv420将计算出的upix和vpix取平均值 值,这导致我的mp4中的颜色都不稳定

// make a YUV420J out of BufferedImage pixels
private Picture makeFrame(BufferedImage bi, ColorSpace cs)
{   
    DataBuffer imgdata = bi.getRaster().getDataBuffer();
    int[] ypix = new int[imgdata.getSize()];
    int[] upix = new int[ imgdata.getSize() >> 2 ];
    int[] vpix = new int[ imgdata.getSize() >> 2 ];
    int ipx = 0, uvoff = 0;

    for (int h = 0; h < bi.getHeight(); h++) {
        for (int w = 0; w < bi.getWidth();  w++) {
            int elem = imgdata.getElem(ipx);
            int r = 0x0ff & (elem >>> 16);
            int g = 0x0ff & (elem >>> 8);
            int b = 0x0ff & elem;
            ypix[ipx] = ((66 * r + 129 * g + 25 * b) >> 8) + 16;
            if ((0 != w % 2) && (0 != h % 2)) {
                upix[uvoff] = (( -38 * r + -74 * g + 112 * b) >> 8) + 128;
                vpix[uvoff] = (( 112 * r + -94 * g + -18 * b) >> 8) + 128;
                uvoff++;
            }
            ipx++;
        }
    }
    int[][] pix = { ypix, upix, vpix, null };
    // old API
    // return new Picture(bi.getWidth(), bi.getHeight(), pix, ColorSpace.YUV420J);
    return Picture.createPicture(bi.getWidth(), bi.getHeight(), pix, ColorSpace.YUV420J);
}
//使用BuffereImage像素制作YUV420J
专用图片生成框(BuffereImage bi,颜色空间cs)
{   
DataBuffer imgdata=bi.getRaster().getDataBuffer();
int[]ypix=newint[imgdata.getSize()];
int[]upix=newint[imgdata.getSize()>>2];
int[]vpix=newint[imgdata.getSize()>>2];
int ipx=0,uvoff=0;
对于(inth=0;h>>16);
int g=0x0ff&(元素>>>8);
int b=0x0ff&elem;
ypix[ipx]=((66*r+129*g+25*b)>>8)+16;
如果((0!=w%2)和&(0!=h%2)){
upix[uvoff]=(-38*r+-74*g+112*b)>>8)+128;
vpix[uvoff]=((112*r+-94*g+-18*b)>>8)+128;
uvoff++;
}
ipx++;
}
}
int[]pix={ypix,upix,vpix,null};
//旧API
//返回新图片(bi.getWidth(),bi.getHeight(),pix,ColorSpace.YUV420J);
返回Picture.createPicture(bi.getWidth(),bi.getHeight(),pix,ColorSpace.YUV420J);
}
使用0.2.0现在

这个例子是crea
    AWTSequenceEncoder8Bit enc = AWTSequenceEncoder8Bit.create2997Fps(outputMovieFile); 
    int framesToEncode = (int) (29.97 * durationInSeconds);
    java.awt.image.BufferedImage image = ...(some background image)

    for (int frameIndx = 0, x = 0, y = 0, incX = speed, incY = speed; frameIndx < framesToEncode; frameIndx++, x += incX, y += incY) {
        Graphics g = image.getGraphics();
        g.setColor(Color.YELLOW);
        if (x >= image.getWidth() - ballSize) incX = -speed;
        if (y >= image.getHeight() - ballSize) incY = -speed;
        if (x <= 0) incX = speed;
        if (y <= 0) incY = speed;
        g.fillOval(x, y, ballSize, ballSize);
        enc.encodeImage(image);
    }
    enc.finish();