小言_互联网的博客

matplotlib-bilibili,抖音很火的动态数据视频自动生成(第四节)-视频,语音合成

746人阅读  评论(0)

“ matplotlib-bilibili,抖音很火的动态数据视频自动生成(第四节)-视频,语音自动合成”

还记得上一节中我们所提到的数据动态视频吗​?这次,为了让大家更加方便的制作自动生成视频,我们可在excel中自由配置数据,方便大家操作​,并且还增加了视频和语音自动合成的方式​。

 

01— 优化操作

 

第一步​:导入xlrd模块,并定义excel模板

如上就是我们需要的模板​。

接下来我们来处理数据。

 

第二步​:处理excel,返回相同时间的数据​。


  
  1. import operator
  2. import xlrd
  3. ​
  4. f=xlrd.open_workbook( "数据.xls") # 打开excel文件读取数据
  5. sheet=f.sheet_by_index( 0)
  6. ​
  7. def sort_sheet(hour,name='数量'):
  8. list=[]
  9. ncols=sheet.ncols
  10. for i in range( 1,sheet.nrows):
  11. if str(int(sheet.row_values(i)[ 1]))==str(hour):
  12. dict = {}
  13. for j in range(ncols):
  14. dict[sheet.cell_value( 0,j)]=sheet.row_values(i)[j]
  15. list.append(dict)
  16. return sorted(list,key=operator.itemgetter(name)) #升序排列
  17. # return sorted(list, key=operator.itemgetter(name),reverse=True) # 降序排列

第三步​:定义颜色

color={"x":"#adb0ff","y":"#ffb3ff","z":"#90d595","a":"#FDF5E6"}

第四步​:建立图表


  
  1. from matplotlib import animation
  2. from matplotlib import pyplot
  3. from pylab import mpl
  4. ​
  5. mpl.rcParams[ 'font.sans-serif'] =[ "SimHei"]
  6. mpl.rcParams[ 'axes.unicode_minus'] = False
  7. fig, ax = pyplot.subplots() #返回一个包含figure和axes对象的元组,将元组分解为fig和ax两个变量
  8. ​
  9. def graph(num):
  10. ax.clear() #清除,不叠加
  11.     list=sort_sheet(num) #调用上面定义的函数
  12. for i,chart_barh in enumerate(list):
  13. ax.barh(i,chart_barh[ "数量"],color=color[chart_barh[ "颜色"]]) # 绘制水平方向的条形图barh()
  14. ax.text(chart_barh[ "数量"],i,chart_barh[ "名称"],size= 14,weight= 600,ha= 'left',va= 'bottom') #添加文字并设置样式
  15. ax.text( 350, 2, num, size= 20, weight= 600, ha= 'left', va= 'bottom')
  16. ax.xaxis.set_ticks_position( 'top')
  17. ax.set_axisbelow( True)
  18. pyplot.xlim( 0, 300)
  19. pyplot.title( '不同用户的文章点赞情况') #添加图标题
  20. ​
  21. animator=animation.FuncAnimation(fig, graph, frames=range( 1, 24))
  22. pyplot.show() #移动到函数外面,不然不会动态显示

制作结果​:如图

第五步​:视频,音频合成,这里需要引入​os模块。

cmd = "ffmpeg -i %s -i %s %s" % (self.mp3, self.mp4, self.new_mp4)os.system(cmd)

注意:需要安装ffmpeg程序

windows如何安装ffmpeg

02— 完整代码


  
  1. import os
  2. from matplotlib import animation
  3. from matplotlib import pyplot
  4. from pylab import mpl
  5. import operator
  6. import xlrd
  7. ​
  8. class chart(object):
  9. def __init__(self):
  10. f = xlrd.open_workbook( "数据.xls") # 打开excel文件读取数据
  11. self.sheet = f.sheet_by_index( 0)
  12. self.color = { "x": "#adb0ff", "y": "#ffb3ff", "z": "#90d595", "a": "#FDF5E6"}
  13. mpl.rcParams[ 'font.sans-serif'] = [ "SimHei"]
  14. mpl.rcParams[ 'axes.unicode_minus'] = False
  15. self.fig, self.ax = pyplot.subplots() # 返回一个包含figure和axes对象的元组,将元组分解为fig和ax两个变量
  16. self.mp4 = 'E:\\数据动态展示.mp4' #生成视频名称,绝对路径
  17. self.mp3 = 'E:\\5018.mp3' #音频路径
  18. self.new_mp4 = 'E:\\new合成.mp4' #合成的mp4路径
  19. def sort_sheet(self,num,name='数量'):
  20. list=[]
  21. ncols=self.sheet.ncols
  22. for i in range( 1,self.sheet.nrows):
  23. if str(int(self.sheet.row_values(i)[ 1]))==str(num):
  24. dict = {}
  25. for j in range(ncols):
  26. dict[self.sheet.cell_value( 0,j)]=self.sheet.row_values(i)[j]
  27. list.append(dict)
  28. return sorted(list,key=operator.itemgetter( '数量')) #升序排列
  29. # return sorted(list, key=operator.itemgetter(name),reverse=True) # 降序排列
  30. def graph(self,num):
  31. self.ax.clear() #清除,不叠加
  32. list=self.sort_sheet(num)
  33. for i,chart_barh in enumerate(list):
  34. self.ax.barh(i,chart_barh[ "数量"],color=self.color[chart_barh[ "颜色"]]) # 绘制水平方向的条形图barh()
  35. self.ax.text(chart_barh[ "数量"],i,chart_barh[ "名称"],size= 14,weight= 600,ha= 'left',va= 'bottom') #添加文字并设置样式
  36. self.ax.text( 250, 2, str(num)+ ":00时", size= 20, weight= 600, ha= 'left', va= 'bottom')
  37. self.ax.text( 200, 0, "<---巍然不动", size= 16, weight= 600, ha= 'left', va= 'bottom')
  38. self.ax.xaxis.set_ticks_position( 'top')
  39. self.ax.set_axisbelow( True)
  40. pyplot.xlim( 0, 300)
  41. pyplot.title( '不同用户的文章点赞情况') #添加图标题
  42. def mp4_and_mp3(self):
  43. animator = animation.FuncAnimation(self.fig, self.graph, frames=range( 1, 24))
  44. animator.save(self.mp4)
  45. pyplot.show()
  46. cmd = "ffmpeg -i %s -i %s %s" % (self.mp3, self.mp4, self.new_mp4)
  47. os.system(cmd)
  48. if __name__ == '__main__':
  49. chart=chart()
  50. chart.mp4_and_mp3()

相关推荐(个人主页也有哦,赶快收藏起来)​:

动态数据视频(第一节)

bilibili,抖音很火的动态数据视频自动生成(第二节)

matplotlib-bilibili,抖音很火的动态数据视频自动生成(第三节)

matplotlib给女朋友画一个爱心吧,这份满满的爱意,一定要记得收下

欢迎关注我们

 

 

大家一起学编程

以学习为主,兴趣为辅,致力于开发,发现更多好玩有趣的编程技巧以及好玩有趣的编程思路。为喜欢编程和想要学习编程的人找到编程的乐趣和动力。

 


转载:https://blog.csdn.net/qq_39046854/article/details/115983304
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场