提交 d3876bc9 编写于 作者: F feilong

拆分生产GIF,再合并成MP4

上级 90ea69b4
......@@ -11,7 +11,8 @@ def test_build_csdn_ask_top10_tag_race():
)
output = OutputMeta(
path='pub/csdn_ask_top10_month.gif',
path='pub/csdn_ask_top10_month',
ext='gif',
title='CSDN 问答标签月排行榜',
x_label='ask.csdn.net',
y_label='问题数',
......@@ -32,7 +33,8 @@ def test_build_csdn_trend_top10_tag_race():
)
output = OutputMeta(
path='pub/csdn_trends_top10_month.mp4',
path='pub/csdn_trends_top10_month',
ext='gif',
title='CSDN topN指数月排行榜',
x_label='csdn.net/trends',
y_label='指数',
......
import sys
import warnings
import platform
import pandas as pd
import numpy as np
from progress.bar import IncrementalBar
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
......@@ -413,7 +415,7 @@ class BarChartRace:
i += 1
def anim_func(self, i):
print('anim_func:', i)
self.bar.next(1)
plt.xkcd(scale=1, length=100, randomness=2)
if(platform.system() == 'Linux'):
plt.rcParams.update({'font.family': "WenQuanYi Micro Hei"})
......@@ -430,13 +432,15 @@ class BarChartRace:
def init_func():
self.plot_bars(0)
print('total:', len(self.df_values))
total_count = len(self.df_values)
self.bar = IncrementalBar(self.filename, max=total_count)
interval = self.period_length / self.steps_per_period
anim = FuncAnimation(self.fig, self.anim_func, range(len(self.df_values)),
init_func, interval=interval)
try:
if self.html:
print("@anim.to_html5_video...")
ret_val = anim.to_html5_video()
try:
from IPython.display import HTML
......@@ -444,6 +448,7 @@ class BarChartRace:
except ImportError:
pass
else:
print(f"@anim.save({self.filename})")
ret_val = anim.save(
self.filename, fps=self.fps, writer=self.writer)
except Exception as e:
......@@ -459,6 +464,7 @@ class BarChartRace:
raise Exception(message)
finally:
plt.rcParams = self.orig_rcParams
self.bar.finish()
return ret_val
......
import moviepy.editor as mp
def concat_gif_list(gifs, output):
clips = []
for gif in gifs:
clips.append(mp.VideoFileClip(gif))
final = mp.concatenate_videoclips(clips)
final.write_videofile(output)
......@@ -6,6 +6,7 @@ import matplotlib
from src.common.path import ordered_list_json_dir
from src.common.json import load_json, dump_json
from src.common.gif import concat_gif_list
from src.bar_chart_race.chart import bar_chart_race
from dataclasses import dataclass
......@@ -41,6 +42,7 @@ class OutputMeta:
month_count: 绘制月份,用来调试,使用较少的月份快速查看输出效果
'''
path: str
ext: str
title: str
x_label: str
y_label: str
......@@ -60,6 +62,7 @@ class Top:
self.count_field = input.count_field
self.output = output.path
self.ext = output.ext
self.title = output.title
self.x_label = output.x_label
self.y_label = output.y_label
......@@ -69,7 +72,28 @@ class Top:
self.__load_dataframe()
# self.__exit()
self.__config_font()
self.__build_race()
max_rows = self.df.shape[0]
i = 0
j = 0
df = self.df
gifs = []
os.makedirs(self.output, exist_ok=True)
while i < max_rows:
end = i+12
if end >= max_rows:
end = max_rows+1
step = end-i
self.df = df[i:end]
filename = os.path.join(self.output, f'{j}.{self.ext}')
self.__build_race(filename)
gifs.append(filename)
i += step
j += 1
all = f'{self.output}.mp4'
# concat_gif_list(gifs, all)
def __load_dataframe(self):
if self.input_type == 'json_dir':
......@@ -143,10 +167,10 @@ class Top:
else:
matplotlib.rc("font", family='FZFangSong-Z02')
def __build_race(self):
def __build_race(self, filename):
bar_chart_race(
df=self.df,
filename=self.output,
filename=filename,
n_bars=10,
title=self.title,
title_size=24,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册