Python Forum
create a movie with python
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a movie with python
#1
Dear all

I have some binary files with names ('Den_car_resample' +'sdf'+ str(n)+'.dat') where n changes from 0 to 25. 
I wrote a code to read these files and then plot the results with imshow command.
at the last step I want to create a movie . to do that, first, I saved the imshow plots in the .png Format and at the end of the program, I used the avconv command to stitch the images together .
unfortunately, my code creates a movie but it is empty. I don't know the reason exactly but I think, first, I have to define a frame to each plot and then create a movie.
now I have 2 questions:

1- would anyone please help me how can I finally create a movie with this code.
2- is there any method that without saving the figures, directly I create  a movie??

here is the code:

import os
import sys #exit the code at a specific line
import subprocess 
import sdf
import numpy as np
import matplotlib.pyplot as plt 
#import time
#import matplotlib.animation as animation
#from IPython import display
from matplotlib.font_manager import FontProperties
fp = FontProperties('Symbola')
 

##################### information from EPOCH input.deck
nx,ny= 1200, 1600
 
xmin=-100e-6
xmax = 110e-6
ymin = -200e-6
ymax = 200e-6
 
X =np.linspace(xmin,xmax,nx) #Generate linearly spaced vector. The spacing between the points is (x2-x1)/(n-1).
Y =np.linspace(ymin,ymax,ny)
#print len(X)
# print len(X)
 
 
#################
for n in range(0,5):
     nstr = str(n)#.zfill(4)
     #print nstr
######################..... reading Density of proton
 
     filename ="Den_pro_resample" +'_sdf_'+ str(n)+'.dat'
 
     with open(filename, 'rb') as f: #read binary file
     data = np.fromfile(f, dtype='float64', count=nx*ny) #float64 for Double precision float numbers
     Den_pro = np.reshape(data, [ny, nx], order='F')
     Den_pro = np.log10(Den_pro )
     #print Den_pro .max()

########################## Display proton density
     plt.subplot(312)
     fig2 = plt.imshow(Den_pro, extent=[X.min()*1e6, X.max()*1e6, Y.min()*1e6, Y.max()*1e6], vmin=24, vmax=30, cmap='brg',      aspect='auto') #cmap='jet', 'nipy_spectral','hot','gist_ncar'
#plt.suptitle('Den_pro')
     plt.title('sdf '+ str(n)+ '; Time= ' +str(n*50)+ 'ps',color='green', fontsize=15)
     plt.xlabel('x($\mu$m)')
     plt.ylabel('y($\mu$m)')
     plt.text(-80,-40,'Den_proton',color='red', fontsize=15)
     plt.colorbar()
 
     #plt.savefig( 'fig%04d.png' % n, bbox_inches='tight') #('test'+ str(n)+ '.png') 
 
     plt.pause(.1)
     plt.clf() 
 
 
############ create a movie

avconv -framerate 1 -i fig%04d.png -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p movie.mp4".split()
#ffmpeg -r 10 -i fig%04d.png -pix_fmt yuv420p movie.mp4".split()
Reply
#2
There is no way this
avconv -framerate 1 -i fig%04d.png -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p movie.mp4".split()
executes without generating a lot of error messages about unknown variables such as avconv or framerate.

You should be using subprocess...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
To run command line programs from within my script I used subprocess.

First I added import subprocess and then:

subprocess.call("avconv -framerate 1 -i fig%06d.png -c:v libx264 -profile:v high -crf 20".split())

Now, although I can see the saved *.png in my Folder but still I CAN'T create a movie. I don't know what do I have to do to correct the code to create a movie. ANY SUGGESTION???
Reply
#4
Look at MoivePy.
Have used it a couple of times in past,it's a really good project. 

I have used most FFmpeg,which is bye most regarded better than avconv
Here a example i found with avconv.
Quote:Create video from image sequence (really good quality)
avconv -framerate 25 -f image2 -i %04d.png -c:v h264 -crf 1 out.mp4
Here a post where i used FFmpeg with subprocess you can look at.
Reply
#5
(May-23-2017, 02:17 PM)masoud Wrote: To run command line programs from within my script I used subprocess.

First I added import subprocess and then:

subprocess.call("avconv -framerate 1 -i fig%06d.png -c:v libx264 -profile:v high -crf 20".split())

Now, although I can see the saved *.png in my Folder but still I CAN'T create a movie. I don't know what do I have to do to correct the code to create a movie. ANY SUGGESTION???

If you issue the bare avconv command in a terminal/command prompt does it work?

Otherwise the whole thing runs in python, exits normally, and you don't get any error messages and the return code from the avconv call is 0?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video Movie Recommender oslagosboi 3 2,939 Feb-21-2023, 08:23 AM
Last Post: JhonSteve
  memory issue loading movie to numpy array djf123 1 2,247 Nov-07-2019, 06:19 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020