php - 如何使用 php 在 FFmpeg 中获取估计时间?
问题描述
我在我的项目中使用 FFmpeg 来使用 php 覆盖视频。
我已经包含了一个命令执行队列。因此,有时队列中有很多请求,并且需要很长时间才能执行。
这就是为什么,我必须显示用户识别的进度条,创建视频需要多少时间。对于这种情况,我必须估计时间。
也许可以通过输出中显示的其他参数来计算它,例如 fps、比特率或速度。
有任何想法吗?
//DownloadJob.php;
public function __construct($video,$transparent_img,$output_file)
{
$this->video=$video;
$this->transparent_img = $transparent_img;
$this->output_file = $output_file;
$create_at = date('Y-m-d H:i:s');
$this->status =0;
$this->download_id = base64_encode($video);
DB::beginTransaction();
DB::insert('INSERT INTO jobs_download(download_id,image,video,status,output_video,create_time)
VALUES(?,?,?,?,?,?)',[$this->download_id,$this->video,$this->transparent_img,$this->status,$this->output_file,$create_at]);
DB::commit();
}
public function handle()
{
$video=$this->video;
$transparent_img=$this->transparent_img;
$output_file=$this->output_file;
$ready=1;
$failed=2;
$input_video = $video;
$transparent_img = $transparent_img;
try{
$ffmpeg = "C:\\ffmpeg\\bin\\ffmpeg";
$cmd = $ffmpeg . " -i " . $input_video . " -i " . $transparent_img . " -filter_complex 'overlay' " . $output_file;
//Log::info($cmd);
exec($cmd, $output);
if(file_exists($output_file)){
DB::beginTransaction();
DB::update('UPDATE jobs_download SET status=? WHERE download_id =?',[$ready,$this->download_id]);
$this->status = 1;
DB::commit();
if(file_exists($input_video)){
unlink($input_video);
}
if(file_exists($transparent_img)){
unlink($transparent_img);
}
}else{
Log::error('DownloadJob.php failed()',['download_id'=>$this->download_id]);
DB::beginTransaction();
DB::update('UPDATE jobs_download SET status=? WHERE download_id =? ',[$failed,$this->download_id]);
$this->status = 3;
DB::commit();
}
}catch (\Exception $e){
Log::error('DownloadJob.php failed()',['download_id'=>$this->download_id]);
DB::beginTransaction();
DB::update('UPDATE jobs_download SET status=? WHERE download_id =? ',[$failed,$this->download_id]);
$this->status = 3;
DB::commit();
}
}
public function failed()
{
$failed = 2;
Log::error('DownloadJob.php failed()',['download_id'=>$this->download_id]);
DB::beginTransaction();
DB::update('UPDATE jobs_download SET status=? WHERE download_id =? ',[$failed,$this->download_id]);
$this->status = 3;
DB::commit();
}
public function getResponse()
{
return ['download_id' => $this->download_id,'eta_time_sec' => $eta_time_sec];
}
//FFmpegController.php
public function generateVideo(Request $request_body)
{
//Overlay file
$transparent_img=Input::file('transparent_img');
//Main file
$video=Input::file('video');
$output_file = $video_name;
//Send in queue for ffmpeg overlay
$job = new DownloadJob($video_name,$image,$output_file);
$data = $this->dispatch($job);
$dl_url = $job->getResponse();//Get response download_id and estimate time in second
print_r($dl_url);
}
输出
我想在我的输出结果中得到这个答案。
[
'download_id':'NWM1ODAwNDU3NzkxOV92aWRlb19maWxlXzE1NDkyNzExMDkubXA0',
'eta_time_sec':5
]
如何从fps、比特率或速度获得以秒为单位的估计时间,但我不知道..
解决方案
推荐阅读
- javascript - 计算以最大金额购买的最大单位
- node.js - 通过 NodeJs 中的多地理编码器从 Yandex Map API Geocoder 获取坐标
- asp.net-core - Resolve scoped service from singleton service
- flutter - 使用对齐属性时,Flutter Container 不会包装到容器的大小
- helm-jenkins - Helm:如何在 Jenkins values.yaml 中添加未分类标签
- nativescript - Nativescript-vue 实现字体图标不起作用
- android - Android Glide:等待未来并加载
- python - Fastparquet 在使用 dataframe.to_parquet() 时给出“TypeError:预期的 str、字节或 os.PathLike 对象,而不是 _io.BytesIO”
- asp.net-mvc - Microsoft 报告和水晶报告无法下载为 PDF 文件
- r - 如何在 Azure 中将 R 控制台输出作为日志文件