Raw File
make_movie.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#####################################################################################
# This file is part of NS2DDV.                                                      #
#                                                                                   #
# Copyright(C) 2011-2018    C. Calgaro  (caterina.calgaro@math.univ-lille1.fr)      #
#                           E. Creusé   (emmanuel.creuse@math.univ-lille1.fr)       #
#                           T. Goudon   (thierry.goudon@inria.fr)                   #
#                           A. Mouton   (alexandre.mouton@math.univ-lille1.fr)      #
#                                                                                   #
# NS2DDV is free software: you can redistribute it and/or modify it under the terms #
# of the GNU General Public License as published by the Free Software Foundation,   #
# either version 3 of the License, or (at your option) any later version.           #
#                                                                                   #
# NS2DDV is distributed in the hope that it will be useful, but WITHOUT ANY         #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   #
# PARTICULAR PURPOSE. See the GNU General Public License for more details.          #
#                                                                                   #
# You should have received a copy of the GNU General Public License along with      #
# NS2DDV. If not, see <http://www.gnu.org/licenses/>.                               #
#####################################################################################

from base import *

def build_mosaic(inputpngs, nrows, ncols, outputpng):
	if (nrows*ncols < len(inputpngs)):
		print("Problem: "+str(nrows*ncols)+" tiles are provided and "+strlen(inputpngs)+" have to be added")
		exit()

	cmd = "montage "
	for inputpng in inputpngs:
		cmd = cmd + inputpng + " "
	cmd = cmd + '-tile '+str(ncols)+'x'+str(nrows)+' -geometry +0+0 '+ outputpng
	os.system(cmd)
	print("File "+outputpng+" saved")


def encode_video(inputpngs, tmppath, outputmovie, forcesave=False):
	if (not(os.path.isdir(tmppath))):
		os.mkdir(tmppath)
		print("Directory "+tmppath+" created")
		creation = True
	else:
		for filetest in os.listdir(tmppath):
			if (((filetest[0:6] == 'frame_') and (filetest[-4:] == '.png')) and not(forcesave)):
				print("Problem with directory "+tmppath+" : it already exists and contains some files named frame_*.png (may induce conflicts). Please choose another directory")
				exit()
		print("Directory "+tmppath+" already exists and is usable for encoding")
		creation = False

	print(str(len(inputpngs)) + ' frames have to be encoded with FFmpeg')

	nframes = 0
	for inputpng in inputpngs:
		shutil.copyfile(inputpng, tmppath+"/frame_"+str(nframes)+".png")
		#os.system("cp "+inputpng+" "+tmppath+"/frame_"+str(nframes)+".png")
		nframes = nframes+1

	cmd = "ffmpeg -i "+tmppath+"/frame_%d.png "+outputmovie+' -loglevel 8'
	if (forcesave):
		cmd = cmd+' -y'
	os.system(cmd)

	for j in range(nframes):
		os.remove(tmppath+"/frame_"+str(j)+".png")
	if (creation):
		os.rrmdir(tmppath)

	print('File '+outputmovie+' created')
back to top