2011-11-09 112 views
2

我想创建一个屏幕录像机类型的东西,我不知道如何编码多个BufferedImages成可观看的格式,如MP4。我更喜欢纯Java代码,并且我无法使用最终用户在工作之前必须安装的任何东西。我可以获得我需要的所有音频和帧,以了解如何制作视频。Java编码视频

如何使用Java编码音频视频?

回答

1
  • JpegImagesToMovie(使用核心JMF - 无本机)将JPEG图像组转换为Quicktime .MOV。
  • JMF也有应用程序。用于合并声音和音频。
6

您可以使用JCodec(http://jcodec.org)的开源纯Java H.264编解码器。为了BufferedImages序列压缩成MP4文件,使用下面的类:

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); 
    } 
} 

然后你可以使用它像这样:

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(); 
} 
+2

不幸的是JCodec被显著更改,以便您的例子并不编译了。您能否编辑您的帖子并使其与当前版本兼容? – andronix

+1

我也需要当前版本,任何教程发送给我? – sergioBertolazzo