我是ffmpeg的新手。我使用FFmpegFrameGrabber和JavaCPP(版本1.3.3)來抓取mp4視頻文件的數據包。我想將數據包的字節流保存在數據庫中,以便在請求數據時解碼數據包並處理圖像。Java FFmpeg解碼avcodec_decode_video2負AVPacket結果
public static void saveVideoToDB(String pathToVideo){
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(pathToVideo);
grabber.start();
AVPacket pkt;
while ((pkt = grabber.grabPacket()) != null) {
BytePointer data = pkt.data();
data.capacity(pkt.size());
byte[] arr = data.getStringBytes();
//arr = [0, 0, 0, 62, 39, 100, 0, 40, -83, -124.....]
if (pkt.stream_index() == AVMEDIA_TYPE_VIDEO) {
//ToDo: save arr to database
testDecode(arr);
}
}
grabber.close();
logger.info("Import video finished.");
}
在我的代碼我第一次嘗試到了一個驗證的概念解碼包的數據,但我不知道,如果它的工作是這樣的:
public static void testDecode(byte[] data){
AVCodec avCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
AVCodecContext avCodecContext = avcodec_alloc_context3(avCodec);
AVDictionary opts = new AVDictionary();
avcodec_open2(avCodecContext, avCodec, opts);
av_dict_free(opts);
AVFrame avFrame = av_frame_alloc();
AVPacket avPacket = new AVPacket();
av_init_packet(avPacket);
Frame frame = new Frame();
avPacket.pts(AV_NOPTS_VALUE);
avPacket.dts(AV_NOPTS_VALUE);
BytePointer bp = new BytePointer(data);
bp.capacity(data.length);
avPacket.data(bp);
avPacket.size(data.length);
avPacket.pos(-1);
IntBuffer gotPicture = IntBuffer.allocate(1);
boolean doVideo = true;
boolean keyFrames = false;
boolean processImage = true;
AVPacket pkt = avPacket;
if (doVideo) {
int len = avcodec_decode_video2(avCodecContext, avFrame, gotPicture, avPacket);
if (len >= 0 && gotPicture.get(0) != 0
&& (!keyFrames || avFrame.pict_type() == AV_PICTURE_TYPE_I)) {
//ToDo: process image
logger.info("decode success");
}else{
logger.info("decode failed");
}
}
}
avcodec_decode_video2的結果是總是爲負值(-1094995529 =>處理輸入時發現無效數據),並且收到以下錯誤:
[h264 @ 00000000214d11a0]找不到啓動代碼。
[h264 @ 00000000214d11a0]錯誤將輸入拆分爲NAL單元。
下面是從輸入一些元數據:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\user01\Documents\fullstream.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42mp41isomiso2
creation_time : 2017-07-27T11:17:19.000000Z
Duration: 00:55:55.48, start: 0.000000, bitrate: 5126 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1/0x31637661), yuv420p, 1920x1080, 4996 kb/s, 25 fps, 25 tbr, 2500 tbn, 5k tbc (default)
Metadata:
creation_time : 2017-07-27T11:17:19.000000Z
handler_name : VideoHandler
Stream #0:1(eng): Audio: mp3 (mp4a/0x6134706D), 24000 Hz, mono, s16p, 125 kb/s (default)
Metadata:
creation_time : 2017-07-27T11:17:19.000000Z
handler_name : SoundHandler
你不檢查'AVPacket.stream_index'是一個有效的'AVMEDIA_TYPE_VIDEO'流。 – WLGfx
謝謝你的建議。我更新了這個問題,但仍然是同樣的錯誤。 –