[英]Encode a video into h264 using bufferedimages?
我試圖將一大堆bufferedimages(由我的應用程序在運行中創建的預先保存的圖像)轉換為使用java的視頻,並希望有一個可以幫助完成該過程的庫。
我已經探索了許多不同的選項,例如jcodec(沒有關於如何使用它的文檔)。 Xuggler(由於與jdk5及其相關庫的兼容性問題,無法使其運行)。 以及一些文檔很差的其他圖書館。
我正在嘗試找到一個我可以使用的庫,它使用java來(1)通過逐幀寫入bufferedimages來創建h264視頻,並且(2)有文檔,這樣我才能真正弄清楚如何使用dam的東西。
關於我應該研究什么的任何想法?
如果純粹的java源代碼存在於某個可以實現這一目標的地方,我會非常有興趣看到它。 因為我很想看看這個人是如何實現這個功能的,以及我如何使用它!
提前致謝...
以下是使用JCodec的方法:
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
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);
}
}
jcodec
now(jcodec-0.1.9.jar)包括SequenceEncoder
,它直接允許將BufferedImage
寫入視頻流。
我花了一些時間將這個新類的默認導入修復到Eclipse中。 刪除第一個導入后,嘗試(如上所述,我找不到某些類)使用Stanislav的代碼創建我自己的代碼並重新導入我發現錯誤:
import org.jcodec.api.awt.SequenceEncoder;
//import org.jcodec.api.SequenceEncoder;
第二個是完全棄用的,沒有文檔指導我到后者。
相應的方法是:
private void saveClip(Trajectory traj) {
//See www.tutorialspoint.com/androi/android_audio_capture.htm
//for audio cap ideas.
SequenceEncoder enc;
try {
enc = new SequenceEncoder(new File("C:/Users/WHOAMI/today.mp4"));
for (int i = 0; i < BUFF_COUNT; ++i) {
BufferedImage image = buffdFramToBuffdImage(frameBuff.get(i));
enc.encodeImage(image);
}
enc.finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.