1

I use OpenCV in a python script to convert images in PIL format to MP4 videos.

Even though I can play back the videos normally after creating them, I cannot upload them on Twitter or Instagram afterwards.

If after creating the video, I convert the created MP4 video, with an external video converter, to the same MP4 video format, it can now be uploaded to both Twitter and instagram. So the issue must be somehow related to the coding formatting of openCV.

I tried using different codec formats instead of 'mp4v', any fourcc encoding format I found online that is compatible with MP4 videos, but the issue remained.

What else could I try to change in the export settings, to hopefully be able to directly upload videos created with openCV?

Here's the sample code I use:

import cv2
import numpy as np
from PIL import Image as Img

#Create a list of PIL images:
PILimages = []
for  i in range(20):
    digit = str(i)
    if i < 10:
        digit = "0" + digit 
    image = Img.open("animationframes/frame " + digit + ".png")
    PILimages.append(image)

#Convert the PIL image list to OpenCV format: 
def PILimageGroupToOpenCV(imageGroup):
    openCVimages = []
    for image in imageGroup:    
        opencvImage_ = np.asarray(image)
        opencvImage_ = cv2.cvtColor(opencvImage_, cv2.COLOR_RGB2BGR)
        openCVimages.append(opencvImage_)
    return openCVimages

#Save the frames as an MP4 video using openCV:
def SavePILimagesToMp4(images,PathAndName,FPS):
    #Convert the PIL images to numpy and then to OpenCV:
    openCVimages = PILimageGroupToOpenCV(images)
    #Create the new video:
    height,width, layers = openCVimages[0].shape
    size = (width,height)
    video = cv2.VideoWriter(PathAndName, cv2.VideoWriter_fourcc(*'mp4v'), FPS, size)
    #Add the images as frames into the video:
    for i in range(len(openCVimages)):
        video.write(openCVimages[i])
    video.release()

#Save the pil images:
SavePILimagesToMp4(PILimages,"out.MP4",25)
  • 2
    According to this [guide](https://www.descript.com/blog/article/guide-to-instagram-video-sizes-how-to-format-your-ig-posts), there are multiple requirements. The video codec should be H.264 (`mp4v` applies H.263 codec). It looks like [Twitter](https://www.descript.com/blog/article/guide-to-instagram-video-sizes-how-to-format-your-ig-posts) also requires H.264 video codec. Please post a code sample that creates a video file. – Rotem Jun 07 '23 at 20:24
  • Thanks. The code I posted does create a video file. Do you want the part included where I load the PIL images? From what I read online, it's not possible to use H.264 codec for MP4 videos with openCV. – Tricky Devil Jun 07 '23 at 23:22
  • In my answers, I usually write frames with sequential counter as text on solid background (without reading any input image). In your code, we can't see the frame size, we can't see the FPS, and we can't execute your code sample. It is possible to use H.264 codec for MP4 videos with OpenCV. – Rotem Jun 08 '23 at 05:33
  • 1
    `*"avc1"` and you'll get H.264, provided that your opencv comes with an ffmpeg that comes with some H.264 encoder. sometimes, it does. you might have to grab "openh264" manually. – Christoph Rackwitz Jun 08 '23 at 09:41
  • @Rotem the frame rate and size is completely irrelevant, I want it to work with any export settings anyways. I included the PIL import part, so now you can test run it if needed. Feel free to use a standard format 1280x720, 25 FPS. The issue lies with the encoding. – Tricky Devil Jun 08 '23 at 16:51
  • @ChristopRackwitz thank you for the tip! Using the "avc1" fourcc returns this error `Failed to load OpenH264 library: openh264-1.8.0-win64.dll` and `global cap_ffmpeg_impl.hpp:3066 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter`. I tried downloading the openCV.dll from [here](https://github.com/cisco/openh264/releases), using the dll file at the bottom. I placed it inside the OpenCV module next to the opencv_videoio_ffmpeg dll files. But that didn't resolve the issue. How can I use the downloaded openh264 dll file? Also I will create an exe file and distribute the final product. – Tricky Devil Jun 08 '23 at 16:57
  • place it in the ***current working directory***, then restart your python interpreter. if that works, it's merely an issue of placing the DLL in a place where DLLs are looked for. – Christoph Rackwitz Jun 08 '23 at 17:06

1 Answers1

2

"mp4v" represents H.263, MPEG 4 ASP. You might have heard of DivX and XviD. That is that.

Your target sites seem to reject that video format.

If you want H.264, MPEG 4 AVC, you need to ask for "avc1" instead. You can also try asking for "VP80".

OpenCV usually comes with ffmpeg, but that ffmpeg often does not come with an encoder for H.264.

It might support OpenH264, which is provided by Cisco on their Github. You can download the DLL for that. Make sure it's the exact version that OpenCV asks for when it fails to find the DLL.

Failed to load OpenH264 library: openh264-1.8.0-win64.dll
        Please check environment and/or download library: https://github.com/cisco/openh264/releases

[libopenh264 @ 0000018c889e9c40] Incorrect library version loaded
[ERROR:0@0.040] global cap_ffmpeg_impl.hpp:3049 open Could not open codec libopenh264, error: Unspecified error (-22)
[ERROR:0@0.040] global cap_ffmpeg_impl.hpp:3066 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter

You need to make sure the DLL is in a place where it can be found. On Windows, there is a defined algorithm for finding DLLs. Do not place it where it doesn't belong. It does not belong in the system32 directory.

In my experiments, I could place the DLL in the current working directory and it was picked up. It's also supposed to be picked up when it's in the same directory as the executable for the current process (the python process).

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Thank you, it works now! Indeed I did not download the correct dll version. Just for documentation sake, regarding your previous comment, in Pycharm a re-boot of the environment was not necessary, simply running the code again after placing the dll file in the directory worked. – Tricky Devil Jun 08 '23 at 18:23