# I've got eveything working at last, but now, how to change codec of video

**URL:** <https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047>\
**Category:** Uncategorized\
**Created:** [November 19, 2022, 10:41pm UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047 "2022-11-19T22:41:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![3d\_Illusions](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3d_illusions/32/6474_2.png) [@3d\_Illusions](https://forum.opencv.org/u/3d_Illusions)\
**Post date:** [November 19, 2022, 10:41pm UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047/1 "2022-11-19T22:41:28Z")

</div>

I need to change the codec of mp4’s created with cv2.VideoWriter.

I’ve found the list of codes here:

[http://mp4ra.org/#/codecs](http://mp4ra.org/#/codecs)

and I’m currently using mpv4. The issue is the very low bit rate, and I think the codec is incompatible with a lot of website. Is there any way to choose h.264? I can only see h.263

---

<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:** [November 19, 2022, 11:30pm UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047/2 "2022-11-19T23:30:28Z")

</div>

`"avc1"`

bitrate has NO RELATION to the specific codec. you can choose a bitrate.

VideoWriter has some ways to tell ffmpeg (opencv uses ffmpeg) what bitrate to use

opencv is for computer vision, **not** for transcoding video. use ffmpeg directly.

---

<div class="post-metadata">

**Author:** ![3d\_Illusions](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3d_illusions/32/6474_2.png) [@3d\_Illusions](https://forum.opencv.org/u/3d_Illusions)\
**Post date:** [November 20, 2022, 9:09am UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047/3 "2022-11-20T09:09:08Z")

</div>

oh, I had a look at the available arguments for the VideoWriter regarding setting bitrate but couldn’t see any. I’ll have to retry.

Regarding using ffmpeg directly. What would that look like in the code? Currently I’m using:

```auto
        scaledvideo = cv.VideoWriter(destvideo, format, sourcefps, newresolution)        
        while True:
            ret, frame = sourcevideo.read()
            if ret == True:
                b = sr.upsample(frame)
                scaledvideo.write(b)
            else:
                break

```

Would it just be a case of pip installing ffmpeg-python and then replacing cv.VideoWriter with ffmpeg’s own videowriter you mean? Maybe something like:

scaledvideo = ffmpeg.output(stream, destvideo)

---

<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:** [November 20, 2022, 2:03pm UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047/4 "2022-11-20T14:03:31Z")

</div>

don’t bother with `ffmpeg-python`, `python-ffmpeg`, or `ffmpeg` packages on PyPI. the most developed/popular one of them appears abandoned, and it’s a lot of hacking around to harness an ffmpeg subprocess, which isn’t how any of this should be done (technically terrible, wasteful). the most recent one is just another amateur attempt to get fame (i.e. the “library” is excrement).

you could look at `imageio`. it does video too. [Imageio Usage Examples — imageio 2.33.1 documentation](https://imageio.readthedocs.io/en/stable/examples.html)

or use `PyAV`. that gives you proper bindings for ffmpeg, i.e. the entire power and flexibility of ffmpeg. available on PyPI. the github repo comes with examples for everything.

> [@crackwitz](#):
>
> VideoWriter has some ways to tell ffmpeg…

by that I meant there is some code in OpenCV that reads an environment variable (that you set in code) to gather up extra arguments for ffmpeg to pass along. AFAIK that’s undocumented or poorly documented.

basically… give `imageio` a look first.

---

<div class="post-metadata">

**Author:** ![3d\_Illusions](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3d_illusions/32/6474_2.png) [@3d\_Illusions](https://forum.opencv.org/u/3d_Illusions)\
**Post date:** [November 20, 2022, 4:34pm UTC](https://forum.opencv.org/t/ive-got-eveything-working-at-last-but-now-how-to-change-codec-of-video/11047/5 "2022-11-20T16:34:30Z")

</div>

Oh wow. Thanks very much, this knowledge is golden. Thanks for sharing 👍
