Encoder

162 views
Skip to first unread message

shatt...@gmail.com

unread,
Aug 22, 2016, 9:10:55 PM8/22/16
to javacv
Hey!

I tried FFmpegFrameRecorder but it only worked with .avi and without setting the codec. The output file is too big I want better compression. If I set a codec I get the errors "Could not open video codec" or "Could not encode video packet".

I got these jars on the classpath:
ffmpeg-3.1.2-1.2.jar
ffmpeg-3.1.2-1.2-windows-x86.jar
javacpp-1.2.3.jar
javacv-1.2.jar

Do I need any extra binaries to use the codecs in avcodec class? For example I want to use AV_CODEC_ID_H264 (28). 

Thanks for the help!

Adam

Frank Karlstrøm

unread,
Aug 24, 2016, 5:07:19 PM8/24/16
to javacv
I have a slightly older version, but have the same libraries as you. 
What kind of error do you get? For Some extra info you should make sure javacv and ffmpeg logs correctly.
For javacv:
System.setProperty("org.bytedeco.javacpp.logger", "slf4j"); //set your desired level
For ffmpeg:
org.bytedeco.javacpp.avutil.av_log_set_level(avutil.AV_LOG_DEBUG); //debug is quite nice, it does not log so much, so debug is used in production

This is my code, that works OK (javacv 1.1 with friends):
For more info on H264 in FFMpeg, the presets, and fine tuning (if you are hardcore h264 expert) see:
https://trac.ffmpeg.org/wiki/Encode/H.264


FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(fileName, width, height, 0);
//Definition for H264
recorder.setVideoOption("crf", "29"); // Constant Rate Factor (23 is the default)
recorder.setVideoOption("preset", "fast"); // fast is actually fast, and gives better compression than normal on my type of video
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4"); // just the file format
// TODO We should use the frame rate from the original recording.
recorder.setFrameRate(15d); // FPS (frames per second)
recorder.setGopSize(30); // I really do not remember what this is, and i do not think it is necessary

Yours, Frank

Samuel Audet

unread,
Aug 28, 2016, 7:09:12 AM8/28/16
to jav...@googlegroups.com
Could you also provide the error message that you get on the console? It
should help diagnose this problem. Thanks!

Samuel

shatt...@gmail.com

unread,
Aug 28, 2016, 6:49:03 PM8/28/16
to javacv
I tried what Frank said.

FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(new File("D:/new.mp4"), grabber.getImageWidth(), grabber.getImageHeight());
recorder.setVideoOption("crf", "29"); 
recorder.setVideoOption("preset", "fast"); 
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4"); 
recorder.setFrameRate(grabber.getFrameRate());
recorder.setFrameRate(15d);
recorder.start();

This is what I get:
org.bytedeco.javacv.FrameRecorder$Exception: avcodec_open2() error -542398533: Could not open video codec.
at org.bytedeco.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:590)
at org.bytedeco.javacv.FFmpegFrameRecorder.start(FFmpegFrameRecorder.java:294)
at VideoPlayer.start(VideoPlayer.java:48)
at VideoEditorFrame$6.run(VideoEditorFrame.java:310)
at java.lang.Thread.run(Thread.java:745)

Samuel Audet

unread,
Aug 28, 2016, 8:20:27 PM8/28/16
to jav...@googlegroups.com
Yes, ok, but could you also copy/paste the error messages that you get on the console?

If for some reason you do not get anything on the console, try to execute FFmpegLogCallback.set() first. Thank you

Samuel

shatt...@gmail.com

unread,
Aug 29, 2016, 12:40:30 PM8/29/16
to javacv
Oh, I knew I was missing something. :)

The first problem is: height not divisible by 2 (768x503)

Then I tried setting the record frame height +1 pixel and it worked for like a few frames.

Samuel Audet

unread,
Aug 29, 2016, 6:04:31 PM8/29/16
to jav...@googlegroups.com
It looks like you're trying to set negative timestamps: That won't work.
Reply all
Reply to author
Forward
0 new messages