how to use ffmpeg for video?

1,128 views
Skip to first unread message

eggnot

unread,
Nov 25, 2011, 10:44:04 AM11/25/11
to javacv
is there any java \ javacv related examples?

i found only example like this - http://dranger.com/ffmpeg/tutorial01.html
my guess it's outdated. or it's not?

Samuel Audet

unread,
Nov 25, 2011, 9:56:43 PM11/25/11
to jav...@googlegroups.com
On 2011-11-26 00:44, eggnot wrote:
> is there any java \ javacv related examples?

You may want to check out the source code of FFmpegFrameGrabber and
FFmpegFrameRecorder:
http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/googlecode/javacv/FFmpegFrameGrabber.java
http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/googlecode/javacv/FFmpegFrameRecorder.java

> i found only example like this - http://dranger.com/ffmpeg/tutorial01.html
> my guess it's outdated. or it's not?

Well, the API of FFmpeg is always changing, but it's a good guide I think

Samuel

eggnot

unread,
Nov 25, 2011, 10:33:58 PM11/25/11
to javacv
oh, thanks! it's much better. i was stuck to get rgb Image, now i see.

1. will you plan later to split wrappers such as for ffmpeg stuff to
separate packages? i mean it's not depended on opencv and can be used
widely.
2. as i read you switch from jna to java-cpp also because of speed. is
it really faster or execution speed not so different? i ask because i
thinking is it worth or not to switch my wrapper to java-cpp.


On 26 ноя, 08:56, Samuel Audet <samuel.au...@gmail.com> wrote:
> On 2011-11-26 00:44, eggnot wrote:
>
> > is there any java \ javacv related examples?
>
> You may want to check out the source code of FFmpegFrameGrabber and

> FFmpegFrameRecorder:http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/go...http://code.google.com/p/javacv/source/browse/trunk/javacv/src/com/go...
>
> > i found only example like this -http://dranger.com/ffmpeg/tutorial01.html

Samuel Audet

unread,
Nov 25, 2011, 10:51:21 PM11/25/11
to jav...@googlegroups.com
On 2011-11-26 12:33, eggnot wrote:
> oh, thanks! it's much better. i was stuck to get rgb Image, now i see.
>
> 1. will you plan later to split wrappers such as for ffmpeg stuff to
> separate packages? i mean it's not depended on opencv and can be used
> widely.

The avcodec.java, avformat.java, etc. are independent from OpenCV. If
the javacv*.jar files are too big for your liking, you may extract those
.java, .class, .so, .dll, .dylib files from the JAR files without
violating the license.

> 2. as i read you switch from jna to java-cpp also because of speed. is
> it really faster or execution speed not so different? i ask because i
> thinking is it worth or not to switch my wrapper to java-cpp.

Yes, it's much faster, at least 20 times faster, e.g.: about 10 ns vs
200 ns for a one argument function, but whether that is significant
depends on how heavy your functions are anyway.

Samuel

eggnot

unread,
Nov 28, 2011, 8:16:37 AM11/28/11
to javacv
the FFmpegFrameGrabber.java uses IplImage. but how i can push push
data to java image after swscale?

thanks.

eggnot

unread,
Nov 28, 2011, 8:43:37 AM11/28/11
to javacv
not the best idea i have:
iterate with buffer.get(0...numBytes) to produce int[] baking int from
3 bytes (as i have rgb)
next, ise MemoryImageSource(int[]) as a producer and finally
Toolkit.getDefaultToolkit().createImage()
Message has been deleted

eggnot

unread,
Nov 28, 2011, 10:51:26 AM11/28/11
to javacv
ok, colors are right, but is it the best way to get Image/
BufferedImage? can i get right array without loop?


int[] pixels = new int[numBytes/3];

for(int i=0, b=0; i<numBytes/3; i++,b+=3)
pixels[i] = (buffer.get(b) & 0xFF)<<16 |
(buffer.get(b+1)&0xFF)<<8 |
(buffer.get(b+2)&0xFF);

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, pixels, 0, width);
return image;

Samuel Audet

unread,
Nov 28, 2011, 8:50:07 PM11/28/11
to jav...@googlegroups.com
Do you mean something like IplImage.getBufferedImage() ?
Message has been deleted

eggnot

unread,
Nov 29, 2011, 11:21:00 AM11/29/11
to javacv
IplImage opencv dependent, right?
(at least i have errors trying to use it with have no opencv
installed)

or do you mean i can get the code from IplImage.getBufferedImage() and
other in opencv_core.java?

i see you doing it in flipCopyWithGamma() with for example
IntBuffer.get(), IntBuffer.put() but can't get the idea. or am i
wrong?


thanks,

> > data to java image after swscale?- Скрыть цитируемый текст -
>
> - Показать цитируемый текст -

eggnot

unread,
Nov 29, 2011, 11:46:26 AM11/29/11
to javacv
seems all that function use native asByteBuffer() from IplImage,
right?

how can i have ByteBuffer or so from buffer and numBytes(names are
from FFmpegFrameGrabber.java)

am i get right, i need something to turn pointer(buffer) with
size(numBytes) into ByteBuffer (or byte[] or maybe int[]) ?
other idea is to tell to sws_scale to write to some ecxisting
ByteBuffer/Array with giving buffer as pointer to it...

for both i have no idea how to impliment...

> > - Показать цитируемый текст -- Скрыть цитируемый текст -

Samuel Audet

unread,
Dec 1, 2011, 8:41:36 AM12/1/11
to jav...@googlegroups.com
On 2011-11-30 01:21, eggnot wrote:
> IplImage opencv dependent, right?
> (at least i have errors trying to use it with have no opencv
> installed)
>
> or do you mean i can get the code from IplImage.getBufferedImage() and
> other in opencv_core.java?
>
> i see you doing it in flipCopyWithGamma() with for example
> IntBuffer.get(), IntBuffer.put() but can't get the idea. or am i
> wrong?

Yes, you'll need to do something like that

> seems all that function use native asByteBuffer() from IplImage,
> right?

What function?

> how can i have ByteBuffer or so from buffer and numBytes(names are
> from FFmpegFrameGrabber.java)

what "buffer"? the BytePointer? Just call BytePointer.asBuffer() yes

> am i get right, i need something to turn pointer(buffer) with
> size(numBytes) into ByteBuffer (or byte[] or maybe int[]) ?
> other idea is to tell to sws_scale to write to some ecxisting
> ByteBuffer/Array with giving buffer as pointer to it...
>
> for both i have no idea how to impliment...

We can copy a Buffer into an array with Buffer.get(array), if this want
you need to do.

Samuel

eggnot

unread,
Dec 2, 2011, 6:40:44 AM12/2/11
to javacv
oh, finally i understand!

in FFmpegFrameGrabber.java

buffer = new BytePointer(av_malloc(numBytes));

is just not producing correct BytePointer! that's why it's capacity 0
and buffer.asByteBuffer() is causing errors.

with
buffer = new BytePointer(numBytes);
finally
buffer.asByteBuffer()
works well.

i wondering i stuck with such simple thing.

Samuel Audet

unread,
Dec 2, 2011, 9:59:16 AM12/2/11
to jav...@googlegroups.com
On 2011-12-02 20:40, eggnot wrote:
> oh, finally i understand!
>
> in FFmpegFrameGrabber.java
>
> buffer = new BytePointer(av_malloc(numBytes));
>
> is just not producing correct BytePointer! that's why it's capacity 0
> and buffer.asByteBuffer() is causing errors.
>
> with
> buffer = new BytePointer(numBytes);
> finally
> buffer.asByteBuffer()
> works well.

Depending on the platform, that may not allocate memory that is aligned
well enough for FFmpeg, so we should still use av_malloc() to make sure,
e.g.:
buffer = new BytePointer(av_malloc(numBytes)).capacity(numBytes);
should do the trick

> i wondering i stuck with such simple thing.

Don't worry, this is not simple.. Software development is always like
that :)

Samuel

Reply all
Reply to author
Forward
0 new messages