library(ellmer)library(tidyverse)library(srt)library(openxlsx)library(readxl)library(lares)library(tuneR)library(stringr)# convert mp4 to mp3 using ffmpegsystem("ffmpeg -i video.mp4 audio.mp3")mp3_title="audio.mp3"# Load the MP3 filemp3_file <-readMP3(mp3_title)# Get the duration in secondsduration_mins <- (length(mp3_file@left) / mp3_file@samp.rate)/60
Show the code
# convert mp3 to text using mlx_whisperimport mlx_whisperimport argparsefrom whisper.utils import get_writerspeech_file="audio.mp3"# Using mlx-community/whisper-large-v3-turbo modelresult = mlx_whisper.transcribe(speech_file, path_or_hf_repo="mlx-community/whisper-large-v3-turbo", word_timestamps=True )srt_writer = get_writer("srt",'.')srt_writer(result,'text.srt')srt_writer = get_writer("txt",'.')srt_writer(result,'text.txt')
Show the code
# translate english to chinese using gemini-2.0-flashsrt_txt0=read_srt('text.srt')srt_txt2=srt_txt0$subtitle|>as.character()# translate to Chinesechat_gemini_model_translate<-chat_gemini(system_prompt ="你是一个中文和英文的翻译专家",turns =NULL,# base_url = "https://generativelanguage.googleapis.com/v1beta",api_key = keyring::key_get("google_ai_api_key"),model ="gemini-2.0-flash",#api_args = list(),#echo = NULL)prompt_text=paste0('请联系上下文把以下文字翻译成中文。总句子数量不变。不要多余的反馈。输出格式为:翻译前的文字《---》翻译成英文',srt_txt2)chat_result1=chat_gemini_model_translate$chat(prompt_text)all_result2=unlist(strsplit(chat_result1, "\n"))length(all_result2)srt_txt=srt_txt0 |>mutate(correct_txt=all_result2 |>str_replace('!!!!','')|>str_extract( "(?<=《---》).*") ,all_correct_txt=all_result2)cn_subtitle=srt_txt |>select(n,start,end,subtitle=correct_txt)srt::write_srt(cn_subtitle,"cn.srt",wrap =FALSE)# embed srt to mp4 using ffmpeginput_video <-"video.mp4"subtitle_file <-"cn.srt"output_video <-"output.mp4"ffmpeg_command <-paste0("ffmpeg -i \"", input_video, "\""," -vf \"subtitles=", subtitle_file, ":force_style='Fontsize=20'\""," -c:a copy -c:v libx264 -crf 23 -preset veryfast \"", output_video, "\"")system(ffmpeg_command)# summary using gemini-2.0-flashchat_gemini_model_summary<-chat_gemini(system_prompt ="你是一个中文和英文的语言专家",turns =NULL,# base_url = "https://generativelanguage.googleapis.com/v1beta",api_key = keyring::key_get("google_ai_api_key"),model ="gemini-2.0-flash",#api_args = list(),#echo = NULL)prompt_text=paste0('please do a Abstract:',srt_txt2)Abstract_result_en=chat_gemini_model_summary$chat(prompt_text)Abstract_result_enprompt_text=paste0('请作中文摘要:',srt_txt2)Abstract_result_cn=chat_gemini_model_summary$chat(prompt_text)Abstract_result_cn