Skip to content

Commit

Permalink
修改中文不能显示的bug,更改文字出现时间的算法
Browse files Browse the repository at this point in the history
  • Loading branch information
qihao123 committed Mar 26, 2020
1 parent 92fd324 commit 6feaf65
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added font/heimi.TTF
Binary file not shown.
Binary file removed src/8083d01e-6080-11ea-8aef-005056c00008.mp4
Binary file not shown.
20 changes: 15 additions & 5 deletions src/Audio_Process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@
class Audio_Process():
def __init__(self):
pass
def Audio_Process(self,mp3path):
def Audio_Process(self,mp3path,num):
y, sr = librosa.load(mp3path,sr=None)
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
beat_times = list(librosa.frames_to_time(beats, sr=sr))
beat_times.append(beat_times[-1] + 1)
return beat_times
time = librosa.get_duration(filename='./result.mp3')
#tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
#beat_times = list(librosa.frames_to_time(beats, sr=sr))
#beat_times.append(beat_times[-1] + 1)
a = time/num
b = []
for i in range(num):
b.append(i*a)
return b

if __name__ == '__main__':
num=32
mp3='C:\\Users\\Administrator\\PycharmProjects\\GenVIdeo\\src\\result.mp3'
Audio_Process().Audio_Process(mp3,32)
29 changes: 20 additions & 9 deletions src/CutWorld.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# coding=utf-8

import jieba
import sys
import importlib
importlib.reload(sys)

'''
分词模块
Expand All @@ -11,11 +14,19 @@
class CutWorld():
def __init__(self):
pass
def CutWorld(self,text):
seg_list = jieba.lcut(text)
punct = set(''':!),.:;?]}¢'"、。〉》」』】〕〗〞︰︱︳﹐、﹒
﹔﹕﹖﹗﹚﹜﹞!),.:;?|}︴︶︸︺︼︾﹀﹂﹄﹏、~¢
々‖•·ˇˉ―--′’”([{£¥'"‵〈《「『【〔〖([{£¥〝︵︷︹︻
︽︿﹁﹃﹙﹛﹝({“‘-—_…/\\''')
word_list = list(filter(lambda x: x not in punct, seg_list))
return word_list
def CutWorld(self,text,uuid):
with open(uuid+'.txt', 'w',encoding='utf-8-sig') as f:
seg_list = jieba.lcut(text)
punct = set(''':!),.:;?]}¢'"、。〉》」』】〕〗〞︰︱︳﹐、﹒
﹔﹕﹖﹗﹚﹜﹞!),.:;?|}︴︶︸︺︼︾﹀﹂﹄﹏、~¢
々‖•·ˇˉ―--′’”([{£¥'"‵〈《「『【〔〖([{£¥〝︵︷︹︻
︽︿﹁﹃﹙﹛﹝({“‘-—_…/\\''')
word_list = list(filter(lambda x: x not in punct, seg_list))
for word in word_list:
f.write(word+'\n')
f.close()
return len(word_list)+1
if __name__ == '__main__':
text ='3月9日某某访问某某'
uuid= '1'
CutWorld().CutWorld(text,uuid)
6 changes: 3 additions & 3 deletions src/DataBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def set_status(self,uuid):
'''
if(self.test_connect()):
conn,cursor = self.connect()
sql = "update `word` set `status` = 2 where uuid = %s"
sql = "update `word` set `status` = 1 where uuid = %s"
try:
cursor.execute(sql,(str(uuid)))
conn.commit()
Expand All @@ -116,6 +116,6 @@ def set_status(self,uuid):
return False

if __name__ == '__main__':
pass

#print(DataBase().insert_word('1','3月9日某某对某某作出指示','新闻'))
#print(DataBase().get_word())
print(DataBase().test_connect())
13 changes: 9 additions & 4 deletions src/Gen_Video.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8
# -*- coding: utf-8 -*-
from moviepy.editor import TextClip, CompositeVideoClip, AudioFileClip

'''
视频生成类
生成1280X720的视频
Expand All @@ -17,19 +18,23 @@
class Gen_Video():
def __init__(self):
pass
def Gen_Video(self,beat_times,word_list,mp3path,uuid):
def Gen_Video(self,beat_times,mp3path,uuid):
FONT_URL = '../font/heimi.TTF'
with open(uuid+'.txt', 'r',encoding='utf-8') as f:
text_str = f.read()
word_list = text_str.split('\n')
clips = []
for index, beat_time in enumerate(beat_times[:-1]):
if index >= len(word_list):
break
print(f'{index + 1}/{len(beat_times)}——{word_list[index]}')
text_clip = TextClip(
word_list[index].encode('utf-8'),
word_list[index],
fontsize=320 // 8,
color='white',
size=(320, 640),
method='caption',
font='msyhbd.ttc') \
font=FONT_URL) \
.set_start(beat_time) \
.set_end(beat_times[index + 1])
text_clip = text_clip.set_pos('center')
Expand Down
2 changes: 1 addition & 1 deletion src/TextToAudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def TextToAudio(self,text):
print("tts api error:" + result_str)

print("result saved as :" + save_file)
path = '/src/result.mp3'
path = 'C:\\Users\\Administrator\\PycharmProjects\\GenVIdeo\\src\\result.mp3'
return path

def fetch_token(self):
Expand Down
Binary file modified src/result.mp3
Binary file not shown.
11 changes: 6 additions & 5 deletions src/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8

# -*- coding: utf-8 -*-
from src.DataBase import DataBase
from src.CutWorld import CutWorld
from src.TextToAudio import TextToAudio
Expand All @@ -21,11 +21,12 @@ def get_word(self):
text = DataBase().get_word()
return text
def run(self):
text,uuid = self.get_word()
wordlist = CutWorld().CutWorld(text)
uuid,text = self.get_word()
num = CutWorld().CutWorld(text,uuid)
audio = TextToAudio().TextToAudio(text)
beat_info = Audio_Process().Audio_Process(audio)
Gen_Video().Gen_Video(beat_info,wordlist,audio,uuid)
#audio地址修改为本地音频的绝对路径
beat_info = Audio_Process().Audio_Process(audio,num)
Gen_Video().Gen_Video(beat_info,audio,uuid)

if __name__ == '__main__':
run().run()

0 comments on commit 6feaf65

Please sign in to comment.