import gradio as gr import os # update modelscope os.system("pip install -U modelscope -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html -i https://mirror.sjtu.edu.cn/pypi/web/simple") import datetime from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks from subtitle_utils import generate_srt #获取当前北京时间 utc_dt = datetime.datetime.utcnow() beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16))) formatted = beijing_dt.strftime("%Y-%m-%d_%H") print(f"北京时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 " f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒") #创建作品存放目录 works_path = '../works_audio_video_recognize/' + formatted if not os.path.exists(works_path): os.makedirs(works_path) print('作品目录:' + works_path) inference_pipeline = pipeline( task=Tasks.auto_speech_recognition, model='damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch') def transcript(audiofile, text_file, srt_file): rec_result = inference_pipeline(audio_in=audiofile) text_output = rec_result['text'] with open(text_file, "w") as f: f.write(text_output) srt_output = generate_srt(rec_result['sentences']) with open(srt_file, "w") as f: f.write(srt_output) return text_output, srt_output def audio_recog(audiofile): utc_dt = datetime.datetime.utcnow() beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16))) formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S") print(f"开始时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 " f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒") print("音频文件:" + audiofile) filename = os.path.splitext(os.path.basename(audiofile))[0] text_file = works_path + '/' + filename + '.txt' srt_file = works_path + '/' + filename + '.srt' text_output, srt_output = transcript(audiofile, text_file, srt_file) utc_dt = datetime.datetime.utcnow() beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16))) formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S") print(f"结束时间: {beijing_dt.year}年{beijing_dt.month}月{beijing_dt.day}日 " f"{beijing_dt.hour}时{beijing_dt.minute}分{beijing_dt.second}秒") return text_output, text_file, srt_output, srt_file def video_recog(filepath): filename = os.path.splitext(os.path.basename(filepath))[0] worksfile = works_path + '/works_' + filename + '.mp4' print("视频文件:" + filepath) utc_dt = datetime.datetime.utcnow() beijing_dt = utc_dt.astimezone(datetime.timezone(datetime.timedelta(hours=16))) formatted = beijing_dt.strftime("%Y-%m-%d_%H-%M-%S.%f") # 提取音频为mp3 audiofile = works_path + '/' + formatted + '.mp3' os.system(f"ffmpeg -i {filepath} -vn -c:a libmp3lame -q:a 4 {audiofile}") #识别音频文件 text_output, text_file, srt_output, srt_file = audio_recog(audiofile) # # 给视频添加字幕 # os.system(f"ffmpeg -i {filepath} -i {srt_file} -c:s mov_text -c:v copy -c:a copy {worksfile}") # print("作品:" + worksfile) return text_output, text_file, srt_output, srt_file css_style = "#fixed_size_img {height: 240px;} " \ "#overview {margin: auto;max-width: 400px; max-height: 400px;}" title = "音视频识别 by宁侠" description = "您只需要上传一段音频或视频文件,我们的服务会快速对其进行语音识别,然后生成相应的文字和字幕。这样,您就可以轻松地记录下重要的语音内容,或者为视频添加精准的字幕。现在就来试试我们的音视频识别服务吧,让您的生活和工作更加便捷!" examples_path = 'examples/' examples = [[examples_path + 'demo_shejipuhui.mp4']] # gradio interface with gr.Blocks(title=title, css=css_style) as demo: gr.HTML('''