Javacv: Could I convert raw H.264 stream to video file?

Created on 16 Jun 2016  路  7Comments  路  Source: bytedeco/javacv

Hi,
I got raw H.264 bytes from MediaCodec on Android, and now I want to use FFmpeg to convert those bytes to MPEG1VIDEO format in real time, and push it to the server. How should I do?

it doesn't work in this way.

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder( outfile,  width,  height,  0 );
recorder.setFormat("mpeg1video");

avformat.AVFormatContext ifmt_ctx = avformat.avformat_alloc_context();
avcodec.AVCodec codec = avcodec.avcodec_find_decoder(avcodec.AV_CODEC_ID_H264);
avformat.avformat_new_stream(ifmt_ctx, codec);
recorder.start(ifmt_ctx);

while( ... )  {
            avcodec.AVPacket pkt = new avcodec.AVPacket();
            avcodec.av_init_packet(pkt);
            BytePointer data = new BytePointer(buffer);    // buffer is byte[]
            pkt.data(data);
            pkt.size(len);
            recorder.recordPacket(pkt);
}

I got these in console.

Output #0, mpeg1video, to '/Users/Eric/Downloads/kkk.mpg':
    Stream #0:0: Video: h264 (libx264) (Constrained Baseline), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 30 tbn, 30 tbc
[mpeg1video @ 0x7ff8c483cc00] Encoder did not produce proper pts, making some up.

Note that I saved those raw H.264 stream to a file, and use command line to convert, it just works fine:

ffmpeg -re -i data_file -f mpeg1video -b 5242880 -r 30 output.mpg

Thanks!!!

duplicate enhancement

Most helpful comment

@ele828 I'm having the same problem as you described. did you ever find a solution to link the MediaCodec output to FFMpegRecorder frames?

All 7 comments

Looks fine to me. BTW, it would be easier to use FFmpegFrameGrabber I think.

Actually you want to do transcoding, so you'll to decode the packets to frames and encode them back to packets. Passing just the packets like that won't work.

@saudet , FFmpegFrameGrabber just deal with File, right? But here what I want to do is real-time transcoding, I want to transcode raw H264 stream to MPEG1 stream and push it to server.

BTW, could you please tell me how could I decode those packets to frames? Thanks :)

Ah, so you want support for InputStream and OutputStream. Sure, a lot of people have been asking for that: https://github.com/bytedeco/javacv/issues/95 If you would like to make a contribution, it would be most welcome. Let me know if you start looking into this and you encounter any problems. I can help you with that.

@ele828 I'm having the same problem as you described. did you ever find a solution to link the MediaCodec output to FFMpegRecorder frames?

BTW, if the ffmpeg program is sufficient, with commit https://github.com/bytedeco/javacpp-presets/commit/13ffffd191fa48d9d9d1d8a64e0c4d64c9da1134 it now gets bundled and can be used easily from Java.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fif10 picture fif10  路  3Comments

eldhosengeorge picture eldhosengeorge  路  3Comments

Maleandr picture Maleandr  路  3Comments

ahmedaomda picture ahmedaomda  路  4Comments

Bahramudin picture Bahramudin  路  3Comments