14

I am writing a python/django application and it needs to do image manipulation and then combine the images into a video (each image is a frame). Image manipulation is easy. I'm using PIL, but for the convert to video part, I'm stuck. I've found pyffmpeg but that just seems to do decoding of videos to frames, not the other way around. Although I may have missed something. I've also heard that pythonMagick (imagemagick wrapper) can do this, but I can't seem to find anything about encoding in the docs.

This is running on a linux server and must be python (since that is what the application is in).

What should I use?

lovefaithswing
  • 1,510
  • 1
  • 21
  • 37

4 Answers4

14

First install ffmpeg using the following command: sudo apt install ffmpeg

import os
os.system("ffmpeg -f image2 -r 1/5 -i ./images/swissGenevaLake%01d.jpg -vcodec mpeg4 -y ./videos/swissGenevaLake.mp4")

Details:

The aforementioned code creates a video from three images file, stored in the images folder. Each image plays for 5 seconds in the video (because of the argument: -r 1/5). The three files names are: "swissGenevaLake1.jpg", "swissGenevaLake2.jpg" and "swissGenevaLake3.jpg" in images folder.

Hope this helps.

Ehsan
  • 1,338
  • 14
  • 13
10

Use OpenCV and python binding. There is cv.WriteFrame function. Similar question and answer

Community
  • 1
  • 1
Andrey Sboev
  • 7,454
  • 1
  • 20
  • 37
  • Well I setup a bounty for this question without looking through the answers thoroughly. When I did a quick check of OpenCV I thought it was for analyzing frames and not creating videos. I did a test though with similar code to the linked answer and created a video very easily. – Dan Roberts May 06 '11 at 12:36
1

You could use Popen just to run the ffmpeg in a subprocess.

Andrew Cox
  • 10,672
  • 3
  • 33
  • 38
0

If u have a folder of images to be framed as video. You can tune the parameters and arrange the frames in sorted manner.

import cv2
import os
from tqdm import tqdm
import glob
#TODO
image_folder = '<Enter your target frames folder here>/*'
video_name = 'Dir to store the video'#save as .avi
#is changeable but maintain same h&w over all  frames
width=640 
height=400 
#this fourcc best compatible for avi
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
video=cv2.VideoWriter(video_name,fourcc, 2.0, (width,height))



for i in tqdm((sorted(glob.glob(image_folder),key=os.path.getmtime))):
     x=cv2.imread(i)
     video.write(x)

cv2.destroyAllWindows()
video.release()
user760664
  • 171
  • 2
  • 5