# Video encode using c++

**URL:** <https://forum.opencv.org/t/video-encode-using-c/9157>\
**Category:** C++\
**Tags:** videoio, programming\
**Created:** [June 13, 2022, 12:42pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157 "2022-06-13T12:42:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 13, 2022, 12:42pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/1 "2022-06-13T12:42:20Z")

</div>

Hello,

I need to try to encode the video into H264 using OpenCV using C++, Can anyone share some reference to perform H264 video encode.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 13, 2022, 1:01pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/2 "2022-06-13T13:01:39Z")

</div>

Hi,  
I think it in the doc [here](https://docs.opencv.org/4.x/d7/d9e/tutorial_video_write.html)

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 12:13pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/3 "2022-06-16T12:13:24Z")

</div>

Hello,

When I try to run the copy and run the code, it pops with a message “Not Enough Parameters”.

 ![createVideo](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/178c9a5833b8850777e492ffc2f341951d216dd9.jpeg)

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 12:21pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/5 "2022-06-16T12:21:55Z")

</div>

```auto
------------------------------------------------------------------------------
This program shows how to write video files.
You can extract the R or G or B color channel of the input video.
Usage:
./video-write <input_video_name> [R | G | B] [Y | N]
------------------------------------------------------------------------------

Not enough parameters

C:\Naveen\projects\ocv\repo\DMS\x64\Debug\DMS.exe (process 19812) exited with code -1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

```

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 16, 2022, 12:26pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/6 "2022-06-16T12:26:29Z")

</div>

did you try :

```auto
For example, a valid command line would look like:

video-write.exe video/Megamind.avi R Y

```

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 12:29pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/7 "2022-06-16T12:29:12Z")

</div>

do i need to download an video-write.exe? I am using the video in .mp4 format hope that should support.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 16, 2022, 12:35pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/8 "2022-06-16T12:35:21Z")

</div>

Sorry but I do not understand  
You post an error message about video\_write.exe so You already got video-write.exe !?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 16, 2022, 12:38pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/9 "2022-06-16T12:38:25Z")

</div>

seriously consider to use ffmpeg (on the cmdline) not opencv to re-encode an existing video file.

and that demo program won’t do, what you wanted (it’s writing only a single color channel to the video)

last, iirc _writing_ h264 still has a patent issue, i’m not sure if e.g. opencv’s windows ffmpeg wrapper has it.

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 12:41pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/10 "2022-06-16T12:41:49Z")

</div>

I dont see any .exe file, after copying the example file from the link you shared, I build and running it, and on the debugger console I see the error as “Not enough parameters”.  
Am I failing to give the inputs, since in the code if(argc!=4) it prints this error, If so, where should I provide this input?

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 12:43pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/11 "2022-06-16T12:43:23Z")

</div>

I am new to this openCV, what you mean by consider using ffmpeg on the cmdline not opencv?  
I am just copying the code, build and run the code.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 16, 2022, 12:58pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/12 "2022-06-16T12:58:03Z")

</div>

> [@navins.2006](#):
>
> what you mean by consider using ffmpeg on the cmdline

there are ready made, well tested [programs](https://ffmpeg.org/) for your problem.  
dont abuse a computer-vision library for it (it’s not meant to do that)

and again, that sample code will require a lot of hacking, before you can use it.  
you’ll need to throw out the split / merge part (and the resp. args) and write the original frame instead

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 1:01pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/13 "2022-06-16T13:01:08Z")

</div>

I appreciate your help in sharing the information.

can you please share some link on step by step procedure to use the ffmpeg?  
I saw the link which you shared, Should I download ffmpeg, if so after downloading how to use it?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 16, 2022, 1:03pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/14 "2022-06-16T13:03:52Z")

</div>

[https://ffmpeg.org/ffmpeg.html](https://ffmpeg.org/ffmpeg.html)

and if that’s not enough, try a search engine.

or perhaps you might like to use a graphical video editor instead? or just a video converter like Handbrake?

- [Video editing software - Wikipedia](https://en.wikipedia.org/wiki/Video_editing_software)
- [Comparison of video converters - Wikipedia](https://en.wikipedia.org/wiki/Comparison_of_video_converters)
- [Comparison of video editing software - Wikipedia](https://en.wikipedia.org/wiki/Comparison_of_video_editing_software)

in this forum we don’t teach how to use computers. that’s not the purpose of this forum.

if you **really** want to …

> [@navins.2006](#):
>
> encode the video into H264 using OpenCV using C++

then you first need to know some C++, which includes knowing what your editor/IDE does.

if you simply click “run”, it may run your program, but without command line arguments.

you should know what “command line arguments” are in general, and you should know how to run programs from a terminal/console.

maybe you noticed… you saved that example code as “DMS.cpp” (or something like that), so your IDE made the file `DMS.exe` from that. that is the file you executed. the code talks about _itself_, but it doesn’t know what file you put it in, so it thinks it’s called `video-write`

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 16, 2022, 1:12pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/15 "2022-06-16T13:12:45Z")

</div>

Yes I do understand the forum usage and I dint ask on how to use computer, I know the usage of computer better.  
If someone is new to the topic, it does not mean they are dumb enough on computer world. Each one has their limitations and everyone is well experienced in their own topics.

If you can help, help and make them to move along, if not you dont need to take your damn time to comment to the thread. Just Ignore and proceed with what makes you fascinated.

You might be experienced enough on working this topics and I appreciate for sharing the useful information.

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 22, 2022, 10:43am UTC](https://forum.opencv.org/t/video-encode-using-c/9157/16 "2022-06-22T10:43:42Z")

</div>

Thanks Guys, I explored on ffmpeg and found how powerful it is for video encoding and can achieve more.

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [June 22, 2022, 12:50pm UTC](https://forum.opencv.org/t/video-encode-using-c/9157/17 "2022-06-22T12:50:13Z")

</div>

You need to either  
A) Learn how to pass parameter to the program when it is run or debugged in Visual Studio (there is a menu item or dialog somewhere in VS)  
B) Run the executable outside of VS in a Windows console; wherever VS builds the .exe

These are very basic VS things, not related to OpenCV

---

<div class="post-metadata">

**Author:** ![navins.2006](https://avatars.discourse-cdn.com/v4/letter/n/e274bd/32.png) [@navins.2006](https://forum.opencv.org/u/navins.2006)\
**Post date:** [June 23, 2022, 8:05am UTC](https://forum.opencv.org/t/video-encode-using-c/9157/18 "2022-06-23T08:05:05Z")

</div>

I know it either on how to use VS and on how to pass it.
