Page 1 of 2

Record H264

PostPosted: Mon Dec 17, 2012 4:49 pm
by sport_coder
I am creating a website that plays the videos I record using VideoFileWriter.

The problem is that the HTML5 video element does not support MPEG4, which I currently use for recording. I do not want to spend much money on a streaming webserver
so it would really help me if there was a way to record the video in mp4 with h.264 codec. Is there a way to do this? Can somebody point me in the right direction?

Cheers,
Sander

Re: Record H264

PostPosted: Wed Dec 19, 2012 10:04 pm
by sport_coder
After spending a day on google and no answer it seems wise to rephrase my question.

The Aforge.Video.FFMPEG I downloaded from this website does not contain a H.264 codec as far as I can see.
When I type VideoCodec. only options as mpeg4 flv etc show; no h264. I am quite new to C# so I might miss some essential things.

When searching I came across the iSpy project.

http://code.google.com/p/ispyconnect/so ... eo.FFMPEG/

I also contains a Video.FFMPEG library and if I open the VideoCodec.h file I can see a H.264 codec there. As I said, I am quite new to C#
so I wondered how I could make the h.264 encoding work? I was wondering why the H.264 codec was not included in the AForge version
as it seems to be a major codec these days?

Thank you for you support.

Re: Record H264

PostPosted: Thu Dec 20, 2012 9:46 am
by andrew.kirillov
Hello,

You may need to rebuild AForge.Video.FFMPEG.dll to get H264. First you may need to get latest FFMPEG development build. Then have a look at VideoCodec.h - there you will need to add new codec into enumeration. Then have a look at VideoCodec.cpp - you will find there 2 arrays which you will need to update to have H264 definition (similar way like you've seen in iSpy). Then build and enjoy.

Re: Record H264

PostPosted: Thu Dec 20, 2012 6:37 pm
by sport_coder
Thank you for the reply. I am now able to record the video with H.264 codec. Unfortunately it is quite slow and can not keep up with the framerate I used to
record with when using mpeg4. I created a thread that checks if the buffer contains images and than writes these to a file (VideoFileWriter). Is there anything
I can do to increase the speed so that the buffer doesnt pile up?

Re: Record H264

PostPosted: Thu Dec 20, 2012 8:37 pm
by andrew.kirillov
sport_coder wrote:Is there anything I can do to increase the speed so that the buffer doesnt pile up?

Since AForge.Video.FFMPEG is just a wrapper around FFMPEG, I would suggest checking FFMPEG documentation and/or discussion boards.

Re: Record H264

PostPosted: Sat Dec 29, 2012 3:13 am
by cesarsouza
Hi Andrew!

I've recently stumbled upon this issue as well. I have implemented the support for encoding H264 in the AForge.Video.FFMPEG project. Would you be interested? I've also added support for the FFV1 and FFVHUFF lossless codecs. I can attach a patch on the project's issue tracker.

By the way, I would really really suggest updating the FFmpeg binaries which are distributed along AForge.NET. They are a bit old, and some things have been heavily improved, particularly on what is related to H624 decoding/encoding.

And finally, I also have implemented an option for maintaining correct aspect ratios in the VideoSourcePlayer. Well, actually I couldn't really determine if this was already possible with the current version, but as I couldn't find it I decided to roll my own. If there was indeed no option for doing this, would you be interested in this patch as well?

I am also attaching the patches here for anyone interested. Some of the configuration values in VideoFileWriter have been based from iSpy's settings.

Best regards and happy holidays!
Cesar

Re: Record H264

PostPosted: Sat Dec 29, 2012 10:28 am
by andrew.kirillov
Hello Cesar,

It seems like H264 may still need to wait till it is exposed in AForge.NET framework. I merged your code and gave it a try - result is not good so far. Initially I did a simple test like this one. When video plays result gets worse and worse. Then I tried it on something real - saving frames from camera. But same problem. I tried changing some settings of the codec (those which are done in "if ( codecContex->codec_id == libffmpeg::CODEC_ID_H264 )"), but with no much luck so far. Settings from iSpy did not give much result either. So there must be something else about it ...

Re: Record H264

PostPosted: Sat Dec 29, 2012 2:53 pm
by cesarsouza
Thanks Andrew for taking a look at the issue!

andrew.kirillov wrote:Hello Cesar,

It seems like H264 may still need to wait till it is exposed in AForge.NET framework. I merged your code and gave it a try - result is not good so far. Initially I did a simple test like this one. When video plays result gets worse and worse. Then I tried it on something real - saving frames from camera. But same problem. I tried changing some settings of the codec (those which are done in "if ( codecContex->codec_id == libffmpeg::CODEC_ID_H264 )"), but with no much luck so far. Settings from iSpy did not give much result either. So there must be something else about it ...


Yes; that is why I suggested updating the FFmpeg libraries. It turns out it is almost impossible to properly configure the codec manually, specially because the range intervals for the setting values have been changing over the last year. The latest versions of FFmpeg offers configuration profiles to help on this task. There are profiles for lossless, fast encoding, heavy encoding, which can be used by calling the av_opt_set function. I believe the AForge FFmpeg had been build in November 30, just a few days after the function was introduced; issues may be more stable now. Plus, instead of expose the codec settings for the user, we could expose enums or string identifiers for choosing an encoding profile.

Re: Record H264

PostPosted: Sat Dec 29, 2012 11:03 pm
by cesarsouza
By the way Andrew, did you try changing the bitrate value? I am afraid I didn't encounter the progressively lost quality you mentioned. I have recorded some videos using the code I sent and uploaded them to Youtube. Here is the link:

http://www.youtube.com/watch?v=nZp0ngXb_Ro&hd=1

If the video fails to load automatically in HD, please configure it to the highest quality. Also, Youtube has lowered the quality of the video a bit; when watching locally the quality is slightly better.

Re: Record H264

PostPosted: Sun Dec 30, 2012 10:44 am
by andrew.kirillov
cesarsouza wrote:By the way Andrew, did you try changing the bitrate value?

No. This what I though to try as well. Also may need to try latest version of FFMPEG. Will give at a go somewhere next year ;)

Anyway, few things to note:
1) I was testing other codecs with the default bit rate used in AForge.NET - result are OK, not so awful like in H264 case;
2) Although I was using FFMPEG from November 30 2011, I would say it is still disappointing, that in the 2011 year the library was giving dozen of properties to configure with little amount of documentation. Basically it was leaving H264 to be use by H264 experts. Or by those brave ones who could spend hours playing with different possible configuration values. But if you say things have changed now ...


But, this is FFMPEG – free, but brutal. I remember I was using it somewhere back in 2005 or so. At that point in time it was required to use it in C#, so I tried to approach it through Interop. Well, it was painful. All configurations were done by setting some fields in some structures, there were not function like SetQuality() or something … So it was real pain. And while I was struggling, I found an interesting mailing list, where one guy asked if there is a plan to make FFMPEG more object oriented (so to speak). And the reply was (maybe from one of the developers of the library): “It is already object oriented ...”. I really hope things have changed since that time …