Recording Video on Android from Bitmap

3,541 views
Skip to first unread message

psandmen

unread,
Mar 1, 2014, 7:54:47 AM3/1/14
to jav...@googlegroups.com
Hi,
I have done the follow stuff.

            rec = new FFmpegFrameRecorder(VideoName, NativeWebcam.DEFAULT_IMAGE_WIDTH, NativeWebcam.DEFAULT_IMAGE_HEIGHT, 1);
            rec.setFrameRate(15);               
            rec.setPixelFormat(0);
            rec.start();

 on Draw Canvas, I do

                    videoBitmap.copyPixelsToBuffer(ipImage.getByteBuffer());
                    rec.record(ipImage);
 
The result is a video. But the Framerate is wrong. The player I try, plays all to fast.
At first, I start with framerate 30, and the I try the Framerate 15.
But, that's doesnt work better.

So, how can I reach the correct framerate?
Normal, I get from the video device ~30fps.

Thanks for you help.
Peter

Samuel Audet

unread,
Mar 1, 2014, 11:37:34 PM3/1/14
to jav...@googlegroups.com
Hi,

You're probably getting variable frame rates, and in that case we need
to specify the timestamps manually with setTimestamp(). Check inside the
RecordActivity.java sample for an example of that:
http://code.google.com/p/javacv/source/browse/samples/RecordActivity.java

Samuel

psandmen

unread,
Mar 2, 2014, 5:23:38 AM3/2/14
to jav...@googlegroups.com
Samuel, Thanks.
That works.

Meghal Adesra

unread,
Mar 7, 2014, 5:34:13 AM3/7/14
to jav...@googlegroups.com
Hello ,

I want to have video from the set of images lets say 20 images,
so for that can I use the file FFmpegFrameRecorder and I need the sound to be embedded to it.

so please help in this as I am stuck from a week into what to use to make video from a set of images I have searched a lot but didnt find a good to go so I am asking this question here.

Samuel Audet

unread,
Mar 10, 2014, 9:41:12 AM3/10/14
to jav...@googlegroups.com
Hello,

On 03/07/2014 07:34 PM, Meghal Adesra wrote:
> I want to have video from the set of images lets say 20 images,
> so for that can I use the file FFmpegFrameRecorder and I need the sound to be embedded to it.
>
> so please help in this as I am stuck from a week into what to use to make video from a set of images I have searched a lot but didnt find a good to go so I am asking this question here.

If you want to create a video from images, there is a sample for that here:
http://code.google.com/p/javacv/source/browse/samples/RecordActivity.java

Do you have any issues with that sample?

Samuel

Meghal Adesra

unread,
Mar 10, 2014, 9:51:42 AM3/10/14
to jav...@googlegroups.com
Hello ,

It seems that it will create video from the camera images.

But actually I want to capture the screen as an video so therefore I was thinking of taking screenshots of the screen and create an video of that screenshots.

do you have any source for what I need to do ?
will try out this link and see if I can modify it to my needs.




--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards
Meghal Adesra

www.makkinfotech.com
India : +91 7878027137

Samuel Audet

unread,
Mar 10, 2014, 10:02:26 AM3/10/14
to jav...@googlegroups.com
On 03/10/2014 10:51 PM, Meghal Adesra wrote:
> It seems that it will create video from the camera images.
>
> But actually I want to capture the screen as an video so therefore I was
> thinking of taking screenshots of the screen and create an video of that
> screenshots.
>
> do you have any source for what I need to do ?
> will try out this link and see if I can modify it to my needs.

I don't have anything ready made, no, but it's apparently possible to
capture the screen through the framebuffer with FFmpeg, and thus with
FFmpegFrameGrabber. Some details here:
http://code.google.com/p/javacv/issues/detail?id=429

Samuel

Meghal Adesra

unread,
Mar 10, 2014, 10:07:24 AM3/10/14
to jav...@googlegroups.com
Hello ,

Framebuffer requires root and ffmpeg requires NDK.

can I have some solution with the sdk itself




Samuel

--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samuel Audet

unread,
Mar 10, 2014, 10:16:01 AM3/10/14
to jav...@googlegroups.com
On 03/10/2014 11:07 PM, Meghal Adesra wrote:
> Hello ,
>
> Framebuffer requires root and ffmpeg requires NDK.
>
> can I have some solution with the sdk itself
>

Well, it looks like we can get Bitmap objects:
http://stackoverflow.com/questions/12462944/how-to-take-a-screenshot-of-other-app-programmatically-without-root-permission

So we can convert that to IplImage, something like that:
https://groups.google.com/forum/#!topic/javacv/jxr-1URgM6M

and save it to with FFmpegFrameGrabebr (no, it doesn't require the NDK)

Samuel

Meghal Adesra

unread,
Mar 10, 2014, 10:20:41 AM3/10/14
to jav...@googlegroups.com
Thanks for the help will surely look into the resources you provided and let you know if there is any issue

thanks a lot for the help




Samuel

--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Meghal Adesra

unread,
Apr 12, 2014, 10:35:26 AM4/12/14
to jav...@googlegroups.com
Hello ,

thanks for the help and I have done the first two things but again got stuck at the third :

1.
2.

So we can convert that to IplImage, something like that:
https://groups.google.com/forum/#!topic/javacv/jxr-1URgM6M

3.

and save it to with FFmpegFrameGrabebr (no, it doesn't require the NDK)

how to save the iplimage file that I get through th FFmpegFrameGraber ?

thanks

Samuel Audet

unread,
Apr 12, 2014, 10:47:25 AM4/12/14
to jav...@googlegroups.com
Hello,

On 04/12/2014 11:35 PM, Meghal Adesra wrote:
> and save it to with FFmpegFrameGrabebr (no, it doesn't require the NDK)
>
> how to save the iplimage file that I get through th FFmpegFrameGraber ?

I meant FFmpegFrameRecorder, sorry. Check the Demo class in the
README.txt file for an example of how to use it.

Samuel

Meghal Adesra

unread,
Apr 13, 2014, 8:55:08 AM4/13/14
to jav...@googlegroups.com
 recorder.setCodecID( CODEC_ID_H263);
        recorder.setFormat("3gp");
        recorder.setPixelFormat(  PIX_FMT_YUV420P);
        recorder.start();

I am getting an error in setCodecID an dsetPixelFormat
can I know why can this happen ?




Samuel

--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards
Meghal Adesra

Samuel Audet

unread,
Apr 13, 2014, 9:18:59 AM4/13/14
to jav...@googlegroups.com
On 04/13/2014 09:55 PM, Meghal Adesra wrote:
> | recorder.setCodecID( CODEC_ID_H263);
> recorder.setFormat("3gp");
> recorder.setPixelFormat( PIX_FMT_YUV420P);
> recorder.start();
>
> |
>
> |I am getting an error in setCodecID an dsetPixelFormat
> |
>
> |can I know why can this happen ?|

Have you tried AV_CODEC_ID_H263 and AV_PIX_FMT_YUV420P?

Meghal Adesra

unread,
Apr 13, 2014, 1:26:37 PM4/13/14
to jav...@googlegroups.com
When I tried to convert Bitmap to IplImage in Javacv Android app, I got an error: "Buffered not large enough for pixels"..


--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Meghal Adesra

unread,
Apr 19, 2014, 6:22:25 AM4/19/14
to jav...@googlegroups.com
Hello ,

I am doing as mentioned below want to create a video from the image:

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/test.mp4",256,256);
        try {
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
            recorder.setFormat("mp4");
            recorder.setFrameRate(30);
//            recorder.setPixelFormat(3);
            recorder.setPixelFormat(avutil.PIX_FMT_YUV420P10);
            recorder.setVideoBitrate(1200);
            recorder.startUnsafe();
//            recorder.start();
            for (int i=0;i< 5;i++)
              {
                recorder.record(image);
              }
            recorder.stop();
           }
        catch (Exception e){
            e.printStackTrace();
          }

but I am getting the below mentioned exception and no video file is created.


04-19 15:42:39.550: W/System.err(16761): com.googlecode.javacv.FrameRecorder$Exception: avcodec_open2() error -22: Could not open video codec.
04-19 15:42:39.550: W/System.err(16761):     at com.googlecode.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:492)

Samuel Audet

unread,
Apr 22, 2014, 7:28:52 AM4/22/14
to jav...@googlegroups.com
On 04/14/2014 02:26 AM, Meghal Adesra wrote:
> When I tried to convert Bitmap to IplImage in Javacv Android app, I got an error: "Buffered not large enough for pixels"..

That means exactly what it says: Make the buffer (image) bigger.

Samuel

Meghal Adesra

unread,
Apr 22, 2014, 7:32:11 AM4/22/14
to jav...@googlegroups.com

Hello

I solved this issue but now when I am trying to put audio in it the both are not in sync ?

Samuel Audet

unread,
Apr 22, 2014, 9:20:35 AM4/22/14
to jav...@googlegroups.com
On 04/19/2014 07:22 PM, Meghal Adesra wrote:
> recorder.setPixelFormat(avutil.PIX_FMT_YUV420P10);

I don't think the MPEG4 supports that format. You should try with
AV_PIX_FMT_YUV420P instead.

Samuel

Samuel Audet

unread,
Apr 22, 2014, 9:21:35 AM4/22/14
to jav...@googlegroups.com
On 04/22/2014 08:32 PM, Meghal Adesra wrote:
> Hello
>
> I solved this issue but now when I am trying to put audio in it the both
> are not in sync ?
>

You could try to reduce the bitrate, maybe your CPU isn't powerful enough.

Samuel

Meghal Adesra

unread,
Apr 22, 2014, 9:37:34 AM4/22/14
to jav...@googlegroups.com
I got the example of the audio from the RecordActivity.java of java samples
and the making the bitmaps and video from that bitmaps from the link you gave
and when I tried to do both it seems to be lagging in the voice ,

I tried to capture both differently different file for audio and different file for video and than saw the time of  both and the time of both varies.

video is of 8 seconds and audio of 11seconds.

so I changed the bitrate from 120000 to 70000 and it was same.
what do you say should I reduce it more ?




Samuel

--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samuel Audet

unread,
Apr 26, 2014, 8:03:43 PM4/26/14
to jav...@googlegroups.com
On 04/22/2014 10:37 PM, Meghal Adesra wrote:
> I got the example of the audio from the RecordActivity.java of java samples
> and the making the bitmaps and video from that bitmaps from the link you
> gave and when I tried to do both it seems to be lagging in the voice ,
>
> I tried to capture both differently different file for audio and
> different file for video and than saw the time of both and the time of
> both varies.
>
> video is of 8 seconds and audio of 11seconds.
>
> so I changed the bitrate from 120000 to 70000 and it was same.
> what do you say should I reduce it more ?

Yes, and you could also try to use x264 and it's "ultrafast" preset by
making these calls:

recorder.setVideoCodec(AV_CODEC_ID_H264);
recorder.setVideoOption("preset", "ultrafast");

Samuel

Meghal Adesra

unread,
Apr 27, 2014, 6:32:49 PM4/27/14
to jav...@googlegroups.com
but if i a using recorder.setVideoCodec(AV_CODE
C_ID_H264);
I am getting an error "cannot open video codec"




Samuel

--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samuel Audet

unread,
Apr 28, 2014, 9:45:18 AM4/28/14
to jav...@googlegroups.com
On 04/28/2014 07:32 AM, Meghal Adesra wrote:
> but if i a using recorder.setVideoCodec(AV_CODEC_ID_H264);
> I am getting an error "cannot open video codec"

Please try again with a more recent version of JavaCV, as it happens,
the just released version 0.8, thanks!

Samuel

e.iva...@gmail.com

unread,
Jul 23, 2014, 9:33:02 AM7/23/14
to jav...@googlegroups.com

Hi, 
I'm using JavaCV v.0.8, when i try to youse FFmpegFrameRecorder with 

        recorder.setVideoCodec(AV_CODEC_ID_H264);
        recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        recorder.setFormat("mp4");
I get the following error  avcodec_open2() error -1 

Samuel Audet

unread,
Aug 2, 2014, 7:28:35 AM8/2/14
to jav...@googlegroups.com
Could you try again with JavaCV 0.9? Thanks!

Samuel

Meghal Adesra

unread,
Aug 2, 2014, 10:00:36 AM8/2/14
to jav...@googlegroups.com
Hello ,

I have used it only once and used in this way
hope it helps you.

videoAudiorecorder.setVideoBitrate(20000);
videoAudiorecorder.setFormat("mp4");
videoAudiorecorder.setSampleRate(sampleAudioRateInHz);
videoAudiorecorder.setFrameRate(2);





--

--- You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/GuapyiLAZSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to javacv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages