glt3953 commited on
Commit
7233d8d
·
1 Parent(s): 9adbf01

Upload 10 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/demo_shejipuhui.mp4 filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ import gradio as gr
3
+ import os
4
+ import datetime
5
+
6
+ #获取当前北京时间
7
+ utc_dt = datetime.datetime.utcnow()
8
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
9
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H")
10
+ print(f"北京时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
11
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
12
+ #创建作品存放目录
13
+ works_path = '../works_audio_video_transcribe/' + formatted
14
+ if not os.path.exists(works_path):
15
+ os.makedirs(works_path)
16
+ print('作品目录:' + works_path)
17
+
18
+ modelname = "small"
19
+ model = whisper.load_model(modelname) #tiny、base、small、medium(可用)、large
20
+
21
+ def transcript(audiofile, prompt, output_dir):
22
+ os.system(f"whisper {audiofile} --model {modelname} --language zh --initial_prompt {prompt} --output_dir {output_dir}")
23
+
24
+ def audio_recog(audiofile):
25
+ utc_dt = datetime.datetime.utcnow()
26
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
27
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
28
+ print(f"开始时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
29
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
30
+
31
+ print("音频文件:" + audiofile)
32
+
33
+ prompt = "以下是普通话的句子"
34
+ filename = os.path.splitext(os.path.basename(audiofile))[0]
35
+ output_txt = works_path + '/' + filename + '.txt'
36
+ output_srt = works_path + '/' + filename + '.srt'
37
+
38
+ output_dir = works_path
39
+ transcript(audiofile, prompt, output_dir)
40
+ with open(output_txt, "r") as f:
41
+ text_output = f.read()
42
+ print("text:" + text_output)
43
+ print("text文件:" + output_txt)
44
+
45
+ with open(output_srt, "r") as f:
46
+ srt_output = f.read()
47
+ print("srt:" + srt_output)
48
+ print("srt文件:" + output_srt)
49
+
50
+ utc_dt = datetime.datetime.utcnow()
51
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
52
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
53
+ print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
54
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
55
+
56
+ return text_output, srt_output
57
+
58
+ def video_recog(filepath):
59
+ utc_dt = datetime.datetime.utcnow()
60
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
61
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
62
+ print(f"开始时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
63
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
64
+
65
+ filename = os.path.splitext(os.path.basename(filepath))[0]
66
+ worksfile = works_path + '/works_' + filename + '.mp4'
67
+ print("视频文件:" + filepath)
68
+
69
+ # 提取音频为aac
70
+ audiofile = works_path + '/' + filename + '.mp3'
71
+ os.system(f"ffmpeg -i {filepath} -vn -c:a libmp3lame -q:a 4 {audiofile}")
72
+ print("音频文件:" + audiofile)
73
+
74
+ prompt = "以下是普通话的句子"
75
+ output_txt = works_path + '/' + filename + '.txt'
76
+ output_srt = works_path + '/' + filename + '.srt'
77
+
78
+ output_dir = works_path
79
+ transcript(audiofile, prompt, output_dir)
80
+ with open(output_txt, "r") as f:
81
+ text_output = f.read()
82
+ print("text:" + text_output)
83
+ print("text文件:" + output_txt)
84
+
85
+ with open(output_srt, "r") as f:
86
+ srt_output = f.read()
87
+ print("srt:" + srt_output)
88
+ print("srt文件:" + output_srt)
89
+
90
+ # 给视频添加字幕
91
+ os.system(f"ffmpeg -i {filepath} -i {output_srt} -c:s mov_text -c:v copy -c:a copy {worksfile}")
92
+ print("作品:" + worksfile)
93
+
94
+ utc_dt = datetime.datetime.utcnow()
95
+ beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16)))
96
+ formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S")
97
+ print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 "
98
+ f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒")
99
+
100
+ return text_output, srt_output, worksfile
101
+
102
+ css_style = "#fixed_size_img {height: 240px;} " \
103
+ "#overview {margin: auto;max-width: 400px; max-height: 400px;}"
104
+
105
+ title = "音视频转录 by宁侠"
106
+ description = "您只需要输入一张物体照片和背景图,即可快速完成背景替换,让物体仿佛置身于不同的场景中,实现穿越体验!无论是漫游古代的宫殿,还是探索未来的科技世界,我们的系统能让您轻松达成,让您的想象力得以自由驰骋!快来体验吧,欢迎使用我们的通用换景服务!"
107
+
108
+ examples_path = 'examples/'
109
+ examples = [[examples_path + 'demo_shejipuhui.mp4']]
110
+
111
+ # gradio interface
112
+ with gr.Blocks(title=title, css=css_style) as demo:
113
+ gr.HTML('''
114
+ <div style="text-align: center; max-width: 720px; margin: 0 auto;">
115
+ <div
116
+ style="
117
+ display: inline-flex;
118
+ align-items: center;
119
+ gap: 0.8rem;
120
+ font-size: 1.75rem;
121
+ "
122
+ >
123
+ <h1 style="font-family: PingFangSC; font-weight: 500; font-size: 36px; margin-bottom: 7px;">
124
+ 音视频转录
125
+ </h1>
126
+ <h1 style="font-family: PingFangSC; font-weight: 500; line-height: 1.5em; font-size: 16px; margin-bottom: 7px;">
127
+ by宁侠
128
+ </h1>
129
+ ''')
130
+ gr.Markdown(description)
131
+ video_state = gr.State()
132
+ audio_state = gr.State()
133
+ with gr.Tab("🎥视频转录 Video Transcribe"):
134
+ with gr.Row():
135
+ with gr.Column():
136
+ video_input = gr.Video(label="🎥视频输入 Video Input")
137
+ gr.Examples(['examples/demo_shejipuhui.mp4'], [video_input], label='语音识别示例 ASR Demo')
138
+ video_recog_button = gr.Button("👂视频识别 Recognize")
139
+ video_output = gr.Video(label="🎥视频 Video", visible=False)
140
+ with gr.Column():
141
+ video_text_output = gr.Textbox(label="✏️识别结果 Recognition Result")
142
+ video_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles")
143
+ with gr.Row(visible=False):
144
+ font_size = gr.Slider(minimum=10, maximum=100, value=32, step=2, label="🔠字幕字体大小 Subtitle Font Size")
145
+ font_color = gr.Radio(["black", "white", "green", "red"], label="🌈字幕颜色 Subtitle Color", value='white')
146
+ video_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
147
+
148
+
149
+ video_recog_button.click(video_recog, inputs=[video_input], outputs=[video_text_output, video_srt_output, video_output])
150
+ # video_subtitles_button.click(video_subtitles, inputs=[video_text_input], outputs=[video_output])
151
+
152
+ with gr.Tab("🔊音频转录 Audio Transcribe"):
153
+ with gr.Row():
154
+ with gr.Column():
155
+ audio_input = gr.Audio(label="🔊音频输入 Audio Input", type="filepath")
156
+ gr.Examples(['examples/paddlespeech.asr-zh.wav', 'examples/demo_shejipuhui.mp3'], [audio_input])
157
+ audio_recog_button = gr.Button("👂音频识别 Recognize")
158
+ with gr.Column():
159
+ audio_text_output = gr.Textbox(label="✏️识别结果 Recognition Result")
160
+ audio_srt_output = gr.Textbox(label="📖SRT字幕内容 SRT Subtitles")
161
+ audio_subtitles_button = gr.Button("添加字幕\nGenerate Subtitles", visible=False)
162
+ audio_output = gr.Audio(label="🔊音频 Audio", visible=False)
163
+
164
+ audio_recog_button.click(audio_recog, inputs=[audio_input], outputs=[audio_text_output, audio_srt_output])
165
+ # audio_subtitles_button.click(audio_subtitles, inputs=[audio_text_input], outputs=[audio_output])
166
+
167
+ # start gradio service in local
168
+ demo.queue(api_open=False).launch(debug=True)
examples/.DS_Store ADDED
Binary file (6.15 kB). View file
 
examples/add_subtitles.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from moviepy.editor import *
2
+ #from moviepy.video.VideoClip import VideoClip
3
+ #from moviepy.video.tools.subtitles import SubtitlesClip
4
+ #
5
+ ### 或者直接使用TextClip
6
+ ##subtitle = TextClip("Hello World", fontsize=24, color='white')
7
+ #
8
+ #video = VideoFileClip("demo_shejipuhui.mp4")
9
+ ##subtitles = (SubtitlesClip("demo_shejipuhui.srt", font="Arial", fontsize=24, color='white'))
10
+ #subtitles = (SubtitlesClip("demo_shejipuhui.srt"))
11
+ #
12
+ #final = CompositeVideoClip([video, subtitles])
13
+ #final.write_videofile("output.mp4")
14
+
15
+ #import ffmpeg
16
+ import subprocess
17
+
18
+ command = "ffmpeg -i demo_shejipuhui.mp4 -i demo_shejipuhui.srt -c:s mov_text -c:v copy -c:a copy output.mp4"
19
+ subprocess.call(command, shell=True)
20
+
21
+ #video = ffmpeg.input('demo_shejipuhui.mp4')
22
+ #subtitle = ffmpeg.input('demo_shejipuhui.srt')
23
+ #
24
+ #out = ffmpeg.concat(video, subtitle, v=1, a=1).node
25
+ #out[0].opts = '-codec copy'
26
+ #out[1].opts = '-codec mov_text'
27
+ #
28
+ #out.output('output.mp4').run()
examples/demo_shejipuhui.ass ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [Script Info]
2
+ ; Script generated by FFmpeg/Lavc58.54.100
3
+ ScriptType: v4.00+
4
+ PlayResX: 384
5
+ PlayResY: 288
6
+
7
+ [V4+ Styles]
8
+ Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
9
+ Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0
10
+
11
+ [Events]
12
+ Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
13
+ Dialogue: 0,0:00:00.00,0:00:01.44,Default,,0,0,0,,在我们的设计普惠当中
14
+ Dialogue: 0,0:00:01.44,0:00:03.76,Default,,0,0,0,,有一个我经常津津乐道的项目
15
+ Dialogue: 0,0:00:03.76,0:00:06.08,Default,,0,0,0,,叫寻找远方的美好
16
+ Dialogue: 0,0:00:07.08,0:00:09.08,Default,,0,0,0,,在这样一个 我们叫寻美
17
+ Dialogue: 0,0:00:09.08,0:00:10.64,Default,,0,0,0,,在这样的一个项目当中
18
+ Dialogue: 0,0:00:10.64,0:00:13.24,Default,,0,0,0,,我们把它跟乡村振兴去结合起来
19
+ Dialogue: 0,0:00:13.24,0:00:15.28,Default,,0,0,0,,利用我们的设计的能力
20
+ Dialogue: 0,0:00:15.28,0:00:17.56,Default,,0,0,0,,我们自身员工的设计能力
21
+ Dialogue: 0,0:00:17.56,0:00:19.60,Default,,0,0,0,,我们设计生态伙伴的能力
22
+ Dialogue: 0,0:00:19.60,0:00:22.60,Default,,0,0,0,,帮助乡村振兴当中
23
+ Dialogue: 0,0:00:22.60,0:00:25.64,Default,,0,0,0,,要希望把他的产品推向市场
24
+ Dialogue: 0,0:00:25.64,0:00:27.44,Default,,0,0,0,,把他的农产品 把他的加工产品
25
+ Dialogue: 0,0:00:27.48,0:00:30.04,Default,,0,0,0,,推向市场的这样的伙伴
26
+ Dialogue: 0,0:00:30.04,0:00:31.16,Default,,0,0,0,,做一件事情
27
+ Dialogue: 0,0:00:31.16,0:00:33.76,Default,,0,0,0,,就是帮他们设计一个好的包装物
examples/demo_shejipuhui.mp3 ADDED
Binary file (430 kB). View file
 
examples/demo_shejipuhui.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e8fa2612b7a25e94f8ec3fe96ac88fb01874b8bbed5b7bc10d07ef0555340bc6
3
+ size 4784476
examples/demo_shejipuhui.srt ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 1
2
+ 00:00:00,000 --> 00:00:01,440
3
+ 在我们的设计普惠当中
4
+
5
+ 2
6
+ 00:00:01,440 --> 00:00:03,760
7
+ 有一个我经常津津乐道的项目
8
+
9
+ 3
10
+ 00:00:03,760 --> 00:00:06,080
11
+ 叫寻找远方的美好
12
+
13
+ 4
14
+ 00:00:07,080 --> 00:00:09,080
15
+ 在这样一个 我们叫寻美
16
+
17
+ 5
18
+ 00:00:09,080 --> 00:00:10,640
19
+ 在这样的一个项目当中
20
+
21
+ 6
22
+ 00:00:10,640 --> 00:00:13,240
23
+ 我们把它跟乡村振兴去结合起来
24
+
25
+ 7
26
+ 00:00:13,240 --> 00:00:15,280
27
+ 利用我们的设计的能力
28
+
29
+ 8
30
+ 00:00:15,280 --> 00:00:17,560
31
+ 我们自身员工的设计能力
32
+
33
+ 9
34
+ 00:00:17,560 --> 00:00:19,600
35
+ 我们设计生态伙伴的能力
36
+
37
+ 10
38
+ 00:00:19,600 --> 00:00:22,600
39
+ 帮助乡村振兴当中
40
+
41
+ 11
42
+ 00:00:22,600 --> 00:00:25,640
43
+ 要希望把他的产品推向市场
44
+
45
+ 12
46
+ 00:00:25,640 --> 00:00:27,440
47
+ 把他的农产品 把他的加工产品
48
+
49
+ 13
50
+ 00:00:27,480 --> 00:00:30,040
51
+ 推向市场的这样的伙伴
52
+
53
+ 14
54
+ 00:00:30,040 --> 00:00:31,160
55
+ 做一件事情
56
+
57
+ 15
58
+ 00:00:31,160 --> 00:00:33,760
59
+ 就是帮他们设计一个好的包装物
60
+
examples/demo_shejipuhui.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 在我们的设计普惠当中
2
+ 有一个我经常津津乐道的项目
3
+ 叫寻找远方的美好
4
+ 在这样一个 我们叫寻美
5
+ 在这样的一个项目当中
6
+ 我们把它跟乡村振兴去结合起来
7
+ 利用我们的设计的能力
8
+ 我们自身员工的设计能力
9
+ 我们设计生态伙伴的能力
10
+ 帮助乡村振兴当中
11
+ 要希望把他的产品推向市场
12
+ 把他的农产品 把他的加工产品
13
+ 推向市场的这样的伙伴
14
+ 做一件事情
15
+ 就是帮他们设计一个好的包装物
examples/paddlespeech.asr-zh.wav ADDED
Binary file (160 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai-whisper
2
+ git+https://github.com/openai/whisper.git