2013-03-15 14 views
5

我有一個非常類似的問題,以this question與matplotlib節能散點圖動畫生產空白的視頻文件

,但建議的解決方案並沒有爲我工作。

我已經使用matplotlib動畫模塊設置了動畫散點圖。當它顯示直播時,這工作正常。我想將它保存到avi文件或類似的東西。我寫過的代碼不會出錯,但它生成的視頻只顯示一組空白的軸或黑屏。我做了幾個檢查,數據正在運行,數字更新它只是沒有得到保存到視頻...

我試圖刪除「動畫= True」和「blit = True」this question建議但建議沒有解決問題。

我已經放置了下面的相關代碼,但如果有必要可以提供更多。任何人都可以建議我應該怎麼做才能實現這個目標?

def initAnimation(self): 
     rs, cfgs = next(self.jumpingDataStreamIterator)  
     #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o') 
     self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o', animated=True) 
     return self.scat, 


def updateAnimation(self, i): 
    """Update the scatter plot.""" 
    rs, cfgs = next(self.jumpingDataStreamIterator) 
    # Set x and y data... 
    self.scat.set_offsets(rs[:2,].transpose()) 
    #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], animated=True) 
    # Set sizes... 
    #self.scat._sizes = 300 * abs(data[2])**1.5 + 100 
    # Set colors.. 
    #self.scat.set_array(cfgs[0]) 
    # We need to return the updated artist for FuncAnimation to draw.. 
    # Note that it expects a sequence of artists, thus the trailing comma. 
    matplotlib.pyplot.draw() 
    return self.scat, 

def animate2d(self, steps=None, showEvery=50, size = 25): 
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots() 
    self.axAnimation.set_aspect("equal") 
    self.axAnimation.axis([-size, size, -size, size]) 
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery) 

    self.univeseAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
          self.updateAnimation, init_func=self.initAnimation, 
          blit=True) 
    matplotlib.pyplot.show() 

def animate2dVideo(self,fileName=None, steps=10000, showEvery=50, size=25): 
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots() 
    self.axAnimation.set_aspect("equal") 
    self.axAnimation.axis([-size, size, -size, size]) 
    self.Writer = matplotlib.animation.writers['ffmpeg'] 
    self.writer = self.Writer(fps=1, metadata=dict(artist='Universe Simulation')) 
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery) 

    self.universeAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
          self.updateAnimation, scipy.arange(1, 25), init_func=self.initAnimation) 

    self.universeAnimation.save('C:/universeAnimation.mp4', writer = self.writer) 
+0

您是否可以將此減少到最少量的代碼來重現問題?這顯然是一個更大的類的一部分,並不清楚這是否是獨立代碼的功能。請幫助我們來幫助你。 – tacaswell 2013-03-16 03:36:07

+0

在http://stackoverflow.com/a/14740703/380231中的_exact_代碼是否可以正常工作? – tacaswell 2013-03-16 14:04:52

+1

抱歉,延遲。我找到了一個解決方法,只需保存大量單個圖像,然後調用ffmpeg將它們鏈接在一起。這並不理想,但完成了工作。 當我在這裏運行代碼http://stackoverflow.com/a/14740703/380231我收到以下錯誤: .... 文件「C:\ Python27 \ lib \ site-packages \ matplotlib \ backend_bases .py「,第2093行,print_figure ** kwargs) print_raw 中的文件」C:\ Python27 \ lib \ site-packages \ matplotlib \ backends \ backend_agg.py「,第483行renderer._renderer.write_rgba(filename_or_obj ) RuntimeError:寫入文件時出錯 – user2175850 2013-03-21 22:28:41

回答

0

對不起。我找到了一個解決方法,只需保存大量單個圖像,然後調用ffmpeg將它們鏈接在一起。這並不理想,但完成了工作。 (較大類的一部分)

def easyway(self, frames): 
    ##INSERT CODE TO GET xdata and ydata! 
    for i in range(0, frames): 
     fileName = "videoName"+"%04d.png" % i 
     matplotlib.pyplot.scatter(xdata,ydata,c=coldata, marker='*', 
           s=15.0, edgecolor='none') 
     matplotlib.pyplot.savefig(self.workingDirectory+'temp/'+fileName, dpi=dpi) 
     matplotlib.pyplot.close() 
     ##INSERT CODE TO UPDATE xdata and ydata! 
    self.createVideoFile(fps)#calls FFMpeg to chain together all the pictures 

    if cleanUp:#removes all the picture files created in the process 
     print "temporary picture files being removed..." 
     self.clearDirectory() 
    print "FINISHED" 

def clearDirectory(self,directoryName='temp'): 
    files = glob.glob(self.workingDirectory+directoryName+"/*") 
    for f in files: 
     os.remove(f) 

def createVideoFile(self,fps=3): 
    command="ffmpeg -f image2 -r %s -i %s%s" % (str(fps), self.workingDirectory+'temp/', self.universe.description) 
    command+="%04d.png" 
    command+=" -c:v libx264 -r 30 %s.mp4" % (self.workingDirectory+'videos/'+self.universe.description) 
    print "Running command:" 
    print command 
    p = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE, stderr=subprocess.STDOUT) 
    output = p.communicate()[0] 
    print "output\n"+"*"*10+"\n" 
    print output 
    print "*"*10 
    print "Video file has been written"