FunASR-powered video transcription, subtitle generation, and LLM-assisted clipping tool with a local Gradio UI.
Python ai-toolsai-video-editingasrauto-subtitleschinesecontent-creationfunasrfunclipgradiollmparaformerspeech-recognitionspeech-to-textsubtitles-generatortranscriptionvideo-editingvideo-processingvideo-subtitlesvideo-transcriptionwhisper-alternative
support output_dir
维石210838065ff9
diff --git a/funclip/launch.py b/funclip/launch.pyindex f3915ce..bae7e4a 100644--- a/funclip/launch.py+++ b/funclip/launch.py@@ -1,3 +1,4 @@+import osimport gradio as grfrom funasr import AutoModelfrom videoclipper import VideoClipper@@ -12,34 +13,58 @@ if __name__ == "__main__":)audio_clipper = VideoClipper(funasr_model)- def audio_recog(audio_input, sd_switch, hotwords):- return audio_clipper.recog(audio_input, sd_switch, hotwords=hotwords)+ def audio_recog(audio_input, sd_switch, hotwords, output_dir):+ return audio_clipper.recog(audio_input, sd_switch, hotwords, output_dir=output_dir)- def video_recog(video_input, sd_switch, hotwords):- return audio_clipper.video_recog(video_input, sd_switch, hotwords)+ def video_recog(video_input, sd_switch, hotwords, output_dir):+ return audio_clipper.video_recog(video_input, sd_switch, hotwords, output_dir=output_dir)- def video_clip(dest_text, video_spk_input, start_ost, end_ost, state):- return audio_clipper.video_clip(dest_text, start_ost, end_ost, state, dest_spk=video_spk_input)+ def video_clip(dest_text, video_spk_input, start_ost, end_ost, state, output_dir):+ return audio_clipper.video_clip(+ dest_text, start_ost, end_ost, state, dest_spk=video_spk_input, output_dir=output_dir+ )- def video_clip_addsub(dest_text, video_spk_input, start_ost, end_ost, state, font_size, font_color):- return audio_clipper.video_clip(dest_text, start_ost, end_ost, state, font_size, font_color, add_sub=True, dest_spk=video_spk_input)-- def mix_recog(video_input, audio_input, sd_switch, hotwords):+ def mix_recog(video_input, audio_input, sd_switch, hotwords, output_dir):+ # output_dir here is for funasr model generate+ # both relative and absolute paths are OK+ output_dir = output_dir.strip()+ if not len(output_dir):+ output_dir = None+ else:+ output_dir = os.path.abspath(output_dir)audio_state, video_state = None, Noneif video_input is not None:- res_text, res_srt, video_state = video_recog(video_input, sd_switch, hotwords)+ res_text, res_srt, video_state = video_recog(+ video_input, sd_switch, hotwords, output_dir=output_dir)return res_text, res_srt, video_state, Noneif audio_input is not None:- res_text, res_srt, audio_state = audio_recog(audio_input, sd_switch, hotwords)+ res_text, res_srt, audio_state = audio_recog(+ audio_input, sd_switch, hotwords, output_dir=output_dir)return res_text, res_srt, None, audio_state- def mix_clip(dest_text, video_spk_input, start_ost, end_ost, video_state, audio_state):+ def mix_clip(dest_text, video_spk_input, start_ost, end_ost, video_state, audio_state, output_dir):+ # output_dir here is for moviepy, vedio saving+ # supposed to be absolute path only+ output_dir = output_dir.strip()+ if not len(output_dir):+ output_dir = None+ else:+ output_dir = os.path.abspath(output_dir)if video_state is not None:- clip_video_file, message, clip_srt = audio_clipper.video_clip(dest_text, start_ost, end_ost, video_state, dest_spk=video_spk_input)+ clip_video_file, message, clip_srt = audio_clipper.video_clip(+ dest_text, start_ost, end_ost, video_state, dest_spk=video_spk_input, output_dir=output_dir)return clip_video_file, None, message, clip_srtif audio_state is not None:- (sr, res_audio), message, clip_srt = audio_clipper.clip(dest_text, start_ost, end_ost, audio_state, dest_spk=video_spk_input)+ (sr, res_audio), message, clip_srt = audio_clipper.clip(+ dest_text, start_ost, end_ost, audio_state, dest_spk=video_spk_input, output_dir=output_dir)return None, (sr, res_audio), message, clip_srt++ def video_clip_addsub(dest_text, video_spk_input, start_ost, end_ost, state, output_dir, font_size, font_color):+ return audio_clipper.video_clip(+ dest_text, start_ost, end_ost, state,+ font_size=font_size, font_color=font_color,+ add_sub=True, dest_spk=video_spk_input, output_dir=output_dir+ )# gradio interfacewith gr.Blocks() as funclip_service:@@ -68,6 +93,7 @@ if __name__ == "__main__":with gr.Row():video_sd_switch = gr.Radio(["No", "Yes"], label="👥是否区分说话人 Recognize Speakers", value='No')hotwords_input = gr.Textbox(label="🚒热词 Hotwords")+ output_dir = gr.Textbox(label="文件输出路径 File Output Dir", value=" ")recog_button = gr.Button("👂识别 Recognize")video_text_output = gr.Textbox(label="✏️识别结果 Recognition Result")video_srt_output = gr.Textbox(label="📖SRT字幕内容 RST Subtitles")@@ -90,13 +116,33 @@ if __name__ == "__main__":srt_clipped = gr.Textbox(label="📖裁剪部分SRT字幕内容 Clipped RST Subtitles")recog_button.click(mix_recog,- inputs=[video_input, audio_input, video_sd_switch, hotwords_input],+ inputs=[video_input,+ audio_input,+ video_sd_switch,+ hotwords_input,+ output_dir,+ ],outputs=[video_text_output, video_srt_output, video_state, audio_state])clip_button.click(mix_clip,- inputs=[video_text_input, video_spk_input, video_start_ost, video_end_ost, video_state, audio_state],+ inputs=[video_text_input,+ video_spk_input,+ video_start_ost,+ video_end_ost,+ video_state,+ audio_state,+ output_dir+ ],outputs=[video_output, audio_output, clip_message, srt_clipped])clip_subti_button.click(video_clip_addsub,- inputs=[video_text_input, video_spk_input, video_start_ost, video_end_ost, video_state, font_size, font_color],+ inputs=[video_text_input,+ video_spk_input,+ video_start_ost,+ video_end_ost,+ video_state,+ output_dir,+ font_size,+ font_color,+ ],outputs=[video_output, clip_message, srt_clipped])# start gradio service in localdiff --git a/funclip/videoclipper.py b/funclip/videoclipper.pyindex b772685..cdbcae3 100644--- a/funclip/videoclipper.py+++ b/funclip/videoclipper.py@@ -21,7 +21,7 @@ class VideoClipper():self.funasr_model = funasr_modelself.GLOBAL_COUNT = 0- def recog(self, audio_input, sd_switch='no', state=None, hotwords=""):+ def recog(self, audio_input, sd_switch='no', state=None, hotwords="", output_dir=None):if state is None:state = {}sr, data = audio_input@@ -46,7 +46,8 @@ class VideoClipper():sentence_timestamp=True,return_raw_text=True,is_final=True,- hotword=hotwords)+ hotword=hotwords,+ output_dir=output_dir,)res_srt = generate_srt(rec_result[0]['sentence_info'])state['recog_res_raw'] = rec_result[0]['raw_text']state['timestamp'] = rec_result[0]['timestamp']@@ -54,7 +55,7 @@ class VideoClipper():res_text = rec_result[0]['text']return res_text, res_srt, state- def clip(self, dest_text, start_ost, end_ost, state, dest_spk=None):+ def clip(self, dest_text, start_ost, end_ost, state, dest_spk=None, output_dir=None):# get from stateaudio_input = state['audio_input']recog_res_raw = state['recog_res_raw']@@ -116,12 +117,19 @@ class VideoClipper():res_audio = datareturn (sr, res_audio), message, clip_srt- def video_recog(self, video_filename, sd_switch='no', hotwords=""):+ def video_recog(self, video_filename, sd_switch='no', hotwords="", output_dir=None):video = mpy.VideoFileClip(video_filename)# Extract the base name, add '_clip.mp4', and 'wav'- base_name, _ = os.path.splitext(video_filename)- clip_video_file = base_name + '_clip.mp4'- audio_file = base_name + '.wav'+ if output_dir is not None:+ _, base_name = os.path.split(video_filename)+ base_name, _ = os.path.splitext(base_name)+ clip_video_file = base_name + '_clip.mp4'+ audio_file = base_name + '.wav'+ audio_file = os.path.join(output_dir, audio_file)+ else:+ base_name, _ = os.path.splitext(video_filename)+ clip_video_file = base_name + '_clip.mp4'+ audio_file = base_name + '.wav'video.audio.write_audiofile(audio_file)wav = librosa.load(audio_file, sr=16000)[0]# delete the audio file after processing@@ -133,9 +141,18 @@ class VideoClipper():'video': video,}# res_text, res_srt = self.recog((16000, wav), state)- return self.recog((16000, wav), sd_switch, state, hotwords)+ return self.recog((16000, wav), sd_switch, state, hotwords, output_dir)- def video_clip(self, dest_text, start_ost, end_ost, state, font_size=32, font_color='white', add_sub=False, dest_spk=None):+ def video_clip(self,+ dest_text,+ start_ost,+ end_ost,+ state,+ font_size=32,+ font_color='white',+ add_sub=False,+ dest_spk=None,+ output_dir=None):# get from staterecog_res_raw = state['recog_res_raw']timestamp = state['timestamp']@@ -209,8 +226,18 @@ class VideoClipper():logging.warning("Concating...")if len(concate_clip) > 1:video_clip = concatenate_videoclips(concate_clip)- clip_video_file = clip_video_file[:-4] + '_no{}.mp4'.format(self.GLOBAL_COUNT)- video_clip.write_videofile(clip_video_file, audio_codec="aac", temp_audiofile="video_no{}.mp4".format(self.GLOBAL_COUNT))+ # clip_video_file = clip_video_file[:-4] + '_no{}.mp4'.format(self.GLOBAL_COUNT)+ if output_dir is not None:+ os.makedirs(output_dir, exist_ok=True)+ _, file_with_extension = os.path.split(clip_video_file)+ clip_video_file_name, _ = os.path.splitext(file_with_extension)+ print(output_dir, clip_video_file)+ clip_video_file = os.path.join(output_dir, "{}_no{}.mp4".format(clip_video_file_name, self.GLOBAL_COUNT))+ temp_audio_file = os.path.join(output_dir, "{}_no{}.mp3".format(clip_video_file_name, self.GLOBAL_COUNT))+ else:+ clip_video_file = clip_video_file[:-4] + '_no{}.mp4'.format(self.GLOBAL_COUNT)+ temp_audio_file = clip_video_file[:-4] + '_tempaudio_no{}.mp4'.format(self.GLOBAL_COUNT)+ video_clip.write_videofile(clip_video_file, audio_codec="aac", temp_audiofile=temp_audio_file)self.GLOBAL_COUNT += 1else:clip_video_file = video_filename