Trying to concatenate various "Portrait" videos I took with my mobile phone using the moviepy library. But for some reason, the result is a distorted video. As a TEST CASE, I have even tried to read JUST ONE video clip and re-write it using the concatenate_videoclips method and it still produces a distorted result.
Here is a sample frame from a test video taken with my mobile (resolution on disk: 1920 x 1080 which obviously includes the black background):
Here is the same frame captured from the output video (resolution maintained at 1920 x 1080 but without the black background => distorted image):
Here is the (very simple) code snippet I used:
from moviepy.editor import VideoFileClip, concatenate_videoclips
video_0 = VideoFileClip("test_vid.mp4")
concatenated_clip = concatenate_videoclips([video_0], method="compose") # same result if method="chain"
concatenated_clip.write_videofile("test_vid_concat.mp4")
I can't figure out what the issue is.
This is a bug in moviepy version 1.0.3: the ffmpeg reader doesn't take rotation metadata into account for videos captured on phones. The bug was noted in:
Video file clip width and height do not take rotation metadata into account #1663
and a fix provided in May of this year on the master branch:
Take into account rotation metadata to define video size #577
Version 1.0.3 is still the latest version on PyPI (i.e. version installed by pip), so you need to install the master branch (git clone the repo and run py setup.py install in the source folder) to get the fix.
I ran into the same issue and installing the master branch fixed it for me.
Be forewarned: moviepy is still pretty out of date. It relies on numpy == 1.20 and so you will have to use a lot of older packages. I highly recommend installing in a virtual environment.
Related
I need to convert videos to .mp4 format I used to ffmpeg but it converts for too long. Is there are a way to convert video to .mp4 in python without ffmpeg?
UPD moviepy depends on ffmpeg too (
==
Zulko/moviepy
pip install MoviePy
import moviepy.editor as moviepy
clip = moviepy.VideoFileClip("myvideo.avi")
clip.write_videofile("myvideo.mp4")
As per MoviePy documentation, there is no ffmpeg dependencies:
MoviePy depends on the Python modules Numpy, imageio, Decorator, and tqdm, which will be automatically installed during MoviePy's installation.
ImageMagick is not strictly required, but needed if you want to incorporate texts. It can also be used as a backend for GIFs, though you can also create GIFs with MoviePy without ImageMagick.
PyGame is needed for video and sound previews (not relevant if you intend to work with MoviePy on a server but essential for advanced video editing by hand).
For advanced image processing, you will need one or several of the following packages:
The Python Imaging Library (PIL) or, even better, its branch Pillow.
Scipy (for tracking, segmenting, etc.) can be used to resize video clips if PIL and OpenCV are not installed.
Scikit Image may be needed for some advanced image manipulation.
OpenCV 2.4.6 or a more recent version (one that provides the package cv2) may be needed for some advanced image manipulation.
Matplotlib
I wrote a quick program that will convert all video files of a particular type in a directory to another type and put them in another directory.
I had to install moviepy using Homebrew for it to work rather than rely on PyCharm's package installation.
import moviepy.editor as moviepy
import os
FROM_EXT = "mkv"
TO_EXT = "mp4"
SOURCE_DIR = "/Volumes/Seagate Media/Movies/MKVs"
DEST_DIR = "/Volumes/Seagate Media/Movies/MP4s"
for file in os.listdir(SOURCE_DIR):
if file.lower().endswith(FROM_EXT.lower()):
from_path = os.path.join(SOURCE_DIR, file)
to_path = os.path.join(DEST_DIR, file.rsplit('.', 1)[0]) + '.' + TO_EXT
print(f"Converting {from_path} to {to_path}")
clip = moviepy.VideoFileClip(from_path)
clip.write_videofile(to_path)
I installed OpenCV3.2 + python3.6.1 from this installation guide. (For the paths needed in guide I typed:
Edit: I'm not sure, but I guess that I should install opencv under 3.6, not 3.6.1, please don't use my paths for your installation!
/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin/libpython3.6.dylib
and
ls -d /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m/)
My testing code is completely the same as the tutorial of OpenCV 3.2 documentation, but the output screen show nothing but a title:
import numpy as np
import cv2
img = cv2.imread('1.jpg',0)
cv2.imshow('image', img)
cv2.waitKey(0)
The result:
As the picture shown the content of the image is missing.
and it seems that it has something to do with my python, the icon is broken:
Please help me! I just want to use opencv for my school project...
If you just encounter the same problem, I can solve your problem. But you should take a look before you follow any further steps:
I will recommend you first delete OpenCV 3.2 from your mac.
Don't know how to delele? Here are the steps:
Read this answer first if you want to know what the following command do in details, then run the following command in your Terminal:
$> sudo find / -name "*opencv*" -exec rm -i {} \;
Please read every delete-checking message carefully, or you may delete some of your files containing "opencv" in filename, which may not related to OpenCV but may be your personal files.
Your cv2.so will still alive somewhere in your disk, go checkout where it is with the following command in Terminal:
$> ls -l /usr/local/lib/python3.6/site-packages/
In my case I found the cv2.so at .../python3.6/..., you should press 'tab' at /usr/local/lib/python then check out the site-packages folder for each python version (,which maybe you're about to re-install the OpenCV,) to search cv2.so. if you found it, delete it.
Install OpenCV3.2 with Homebrew for Python2.7 / 3.6. Just follow all the steps, and keep in mind that Homebrew is your best friend.
Edit: The link works for both Python2.7 and 3.6.
I ran into a similar issue but on the C++ API perspective. Credits go out to mattmyne.
Window autosize was not working for macOS using cocoa. The window's image dimensions could not be found. This has been fixed by removing IP64 specific synthesize in window_cocoa.mm that was causing null reference for the window's contentView image property in cvShowImage (image reference was not linked to _image).
In a nutshell, OpenCV tried to support both 32 and 64-bit ObjC compilers but since Apple no longer supports 32-bit, some image synthesizing operations resulted in null references in 64-bit machines. More info
To resolve this, locate the file window_cocoa.mm; if built from source it'll be in opencv/modules/highgui/src.
Change this
#implementation CVView
#if defined(__LP64__)
#synthesize image;
#else // 32-bit Obj-C does not have automatic synthesize
#synthesize image = _image;
#endif
To this
#implementation CVView
#synthesize image = _image;
Do the same thing for the CVWindow and CVSlider implementations to accommodate videos as well.
Recompile OpenCV and test out your code.
I am trying to generate code128 barcodes using Python/Elaphe, which is based on Barcode Writer In Pure Postscript (BWIPP). Strangely, the barcodes generated by Elaphe don't match the ones generated by BWIPP and do not conform to code 128 standard.
In particular, I tried a simple example, the generation of a barcode for the letter 'A' (capital A):
from elaphe import barcode
b = barcode('code128', 'A')
b.show()
That works just fine, but the generated barcode is missing the right part. It is 35 pixels wide, where it should be 46. The left part of the barcode matches the one generated by BWIPP and every other code128 generator - it's only the right section that is missing.
Anyone know what's wrong?
(Using elaphe 0.6.0 with python 2.7.10 on Kubuntu 15.10)
See this bug report:
https://bitbucket.org/whosaysni/elaphe/issues/84/code-128-generation-produces-unreadable
It seems that this bug is fixed in the current source version, also the bug is still marked as new.
The the patch which fixed this bug imho:
https://bitbucket.org/whosaysni/elaphe/commits/19dd8f58c76ac75914e3e4d8ae7db1b9489cbcb8?at=develop
This patch is from the 2014-10-22, the current version elaphe 0.6.0 on pypi is from 2013-12-05. If you installed via pip you have the buggy version.
There is a python3 enabled fork of this project https://pypi.python.org/pypi/elaphe3, which was uploaded on the 2016-05-25. So this fork might contain the necessary bugfix. You could remove elaphe and install elaphe3.
However, considering that elaphe (at least the non 3 version) looks pretty abandoned and has GhostScript and PIL as dependencies I would look for another solution.
i'm trying to utilize PIL to open a jpeg image and assign it to a Tkinter's label.
However whenever i try to open the image i get the same problem as this guy
I tried all the suggestions he got and also the ones i found here but it doesn't seem to fix, by installing PIL or Pillow(i tried that too) during the setup i get :
*** TKINTER support not available
*** JPEG support not available
And whenever i run my code i get an IOError: decoder jpeg not available
I'm using python 2.7.
Can someone provide a good method to make PIL or Pillow work with jpeg support? I've been googling extensively for two days, but all the possible fixes that i found don't seem to work for me
Installing libjpeg-dev should do the trick, as proposed by the link you provided. But if it doesn't help (I ran into that as well) you can consider upgrading to Pillow 3, it looks like you are running Pillow 2. This also helped me getting rid of the errors, don't know why exactly..
I want to record a video with python (using my webcam) and I came across VideoCapture which was easy to install on windows. I know there is OpenCV out there, but that was too much for me.
So far I can create every 0.04 seconds a .jpg with this code:
from VideoCapture import Device
import time
cam = Device(devnum=0) #uses the first webcame which is found
x = 0
while True:
cam.saveSnapshot(str(x)+'.jpg', timestamp=3, boldfont=1) #########################
x += 1
time.sleep(0.04)
0.04 seconds * 25 = 1. So what I am planning to do is an animated gif, that has 25 frames/sec. If somebody of you knows how to produce a real video file like .mp4, I really would prefer the .mp4 rather than .gif. However if thats not possible, the next thing I need to do is, to concatenate all .jpg files (0.jpg, 1.jpg, 2.jpg ...) but as you can imagine with increasing recording time I get A LOT of files. So I was wondering if it would be possible to write the .jpg files (the frames) to one .gif file consecutively. If thats not possible in python, how would you concatenate the jpg files to get an animated gif in the end?
OpenCV will make it more easy. You will find more problem in your method. To use opencv you have to install 1 more package known as numpy (numerical python). It's easy to install. If you want to install it automatically:
Install
Install pip manually
After that go to your cmd>python folder>Lib>site-packages and type pip install numpy *but for using pip you have to be in internet access.
After installation of numpy just type pip intall opencv.
Now you can import all the packages. If numpy somehow fails, maunually downlaod numpy 1.8.0 and install it.