文字转视频好用的工具有哪些?文字图片一键自动朗读转视频文件解放双手,一刀工具箱提供在线文字转语音短视频制作必备工具。
代码片段
//2018.9.22 下载与打包生成视频static function buildMp4($id=''){if($id){$lists = self::where("is_convert",0)->where("id",$id)->get();}else{ //$lists = self::where("is_convert",0)->where("status",self::STATUS_OK)->limit(5)->orderBy("id","asc")->get();}UtilsHelper::debug("COUTN = ".$lists->count());foreach($lists as $row){try {UtilsHelper::debug("Start {$row->id}");$img_srcs = UtilsHelper::jsonDecode($row->images);//检查是否有图片if (!$img_srcs) {$row->fill(["status" => self::STATUS_NO_IMG, "is_convert" => 2])->save();continue;}$content = $row->content;$base_dir = "mp4/{$row->id}";/*** 1、先下载图片* 2、生成mp3* 3、合成mp4*/foreach ($img_srcs as $key => $img) {$full_img = $img;$filename = $key . ".jpeg";if(Str::startsWith($img,"data:image")){UtilsHelper::base64_upload($full_img, $base_dir, "image", $filename);#UtilsHelper::debug("IMAGE_PATH",$res);}else{UtilsHelper::download_image($full_img, $base_dir, '', [], $filename);}$image_dist = storage_path("app/public/$base_dir/{$filename}");transform_image($image_dist, "jpeg", $image_dist);}$mp3_name = $row->id . ".mp3";$mp4_name = ($row->title ?? $row->id) . ".mp4";//生成mp3文件$mp3_path = self::buildMp3($content, $base_dir, $row);if (!$mp3_path) {continue;}$img_base_dir = storage_path("app/public/{$base_dir}");$mp3_base_path = storage_path("app/public/{$base_dir}/{$mp3_name}");$mp4_base_path = storage_path("app/public/{$base_dir}/{$mp4_name}");//合成mp4$time = self::getMp3Time($mp3_base_path);//mp3的时长,mp4要与它一致。$shell_exec = "ffmpeg -loop 1 -threads 2 -framerate 1/6 -y -i {$img_base_dir}/%d.jpeg -i {$mp3_base_path} -vf scale=1280:-2 -shortest -fs 50M -c:a aac -t {$time} {$mp4_base_path} 2>&1";$res = UtilsHelper::shellRun($shell_exec,60);// UtilsHelper::log_debug($shell_exec,$res);$down_url = "{$base_dir}/{$mp4_name}";$row->fill(["is_convert" => 1, "down_url" => $down_url),"is_send"=>$is_send])->save();}catch (\Exception $e){UtilsHelper::log_debug("buildMp4_ERR",$e);}}}