Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could I convert raw H.264 stream to video file? #435

Closed
ele828 opened this issue Jun 16, 2016 · 7 comments
Closed

Could I convert raw H.264 stream to video file? #435

ele828 opened this issue Jun 16, 2016 · 7 comments

Comments

@ele828
Copy link

ele828 commented Jun 16, 2016

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!!!

@saudet
Copy link
Member

saudet commented Jun 16, 2016 via email

@saudet
Copy link
Member

saudet commented Jun 16, 2016

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.

@ele828
Copy link
Author

ele828 commented Jun 16, 2016

@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 :)

@ele828
Copy link
Author

ele828 commented Jun 16, 2016

@saudet
Copy link
Member

saudet commented Jun 16, 2016

Ah, so you want support for InputStream and OutputStream. Sure, a lot of people have been asking for that: #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.

@MarsVard
Copy link

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

@saudet
Copy link
Member

saudet commented Mar 24, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants