2012-01-24 181 views
4

我想製作一個程序,可以編碼和解碼h.264視頻,所以我可以編輯這個視頻。 任何人都可以告訴我如何做到這一點,如果我想在Java中做這個程序?Java - h.264視頻編碼

+0

最終'FFMpeg'可以做到這一點,但我不是很確定。如果確實如此,那麼肯定會有一個Java Wrapper。 – evotopid

+0

嘗試[Xuggler](http://www.xuggle.com/xuggler/)API – JuanZe

+0

現在你提到它,這似乎是一個更好的主意(比試圖使用JMF)。 +1 –

回答

1

獲取或編寫一個可對所需格式進行編碼和解碼的類。確保它是一個服務提供者接口。將它添加到應用程序的運行時類路徑中。使用JMF的默認播放器播放它。不太瞭解編輯它。

0

檢查ffmpeg庫和x264。編譯並撰寫這些庫。它具有非常好的視頻編碼/解碼API。如果你只編譯ffmpeg,那麼你不能編碼h.264,只能解碼。要編碼h264,您需要編譯x264庫。 http://ubuntuforums.org/showthread.php?t=786095。很好的教程。 這是C API,但您可以使用本地方法調用它。

此致敬意,並對不起我的英語。

7

您可以使用JCodec(http://jcodec.org)。

要解碼的視頻序列中去:

int frameNumber = 10000; 
FileChannelWrapper ch = null; 
try { 
    ch = NIOUtils.readableFileChannel(new File(path to mp4)); 
    FrameGrab frameGrab = new FrameGrab(ch); 

    frameGrab.seek(frameNumber); 
    Picture frame; 
    for (int i = 0; (frame = frameGrab.getNativeFrame()) != null && i < 200; i++) { 
     // Do something 
    } 
} finally { 
    NIOUtils.closeQuietly(ch); 
} 

爲了編碼序列:

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(Picture bi) throws IOException { 
     if (toEncode == null) { 
      toEncode = Picture.create(bi.getWidth(), bi.getHeight(), ColorSpace.YUV420); 
     } 

     // Perform conversion (RGB -> YUV) 
     transform.transform(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); 
    } 
} 

終於到轉換爲FROM Android的圖像使用:

public static Picture fromBitmap(Bitmap src) { 
    Picture dst = Picture.create((int)src.getWidth(), (int)src.getHeight(), RGB); 
    fromBitmap(src, dst); 
    return dst; 
} 

public static void fromBitmap(Bitmap src, Picture dst) { 
    int[] dstData = dst.getPlaneData(0); 
    int[] packed = new int[src.getWidth() * src.getHeight()]; 

    src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); 

    for (int i = 0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) { 
     for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) { 
      int rgb = packed[srcOff]; 
      dstData[dstOff]  = (rgb >> 16) & 0xff; 
      dstData[dstOff + 1] = (rgb >> 8) & 0xff; 
      dstData[dstOff + 2] = rgb & 0xff; 
     } 
    } 
} 

public static Bitmap toBitmap(Picture src) { 
    Bitmap dst = Bitmap.create(pic.getWidth(), pic.getHeight(), ARGB_8888); 
    toBitmap(src, dst); 
    return dst; 
} 

public static void toBitmap(Picture src, Bitmap dst) { 
    int[] srcData = src.getPlaneData(0); 
    int[] packed = new int[src.getWidth() * src.getHeight()]; 

    for (int i = 0, dstOff = 0, srcOff = 0; i < src.getHeight(); i++) { 
     for (int j = 0; j < src.getWidth(); j++, dstOff++, srcOff += 3) { 
      packed[dstOff] = (srcData[srcOff] << 16) | (srcData[srcOff + 1] << 8) | srcData[srcOff + 2]; 
     } 
    } 
    dst.setPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); 
} 

FINALLY解碼上,你會得到YUV框架出來,以便將其轉換成RGB幀去:

Transform transform = ColorUtil.getTransform(pic.getColor(), ColorSpace.RGB); 
Picture rgb = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.RGB); 
transform.transform(pic, rgb); 

,並確保您下載JAR所有DEPS:http://jcodec.org/downloads/jcodec-0.1.3-uberjar.jar

+0

謝謝你的回答。編碼操作的計算密集程度如何? – biggvsdiccvs

相關問題