检查php exec的输出总是返回空数组 [英] check the output of php exec always returns empty array

查看:299
本文介绍了检查php exec的输出总是返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换一些电影,并希望确保转换顺利进行:

I'm converting some movies and want to make sure the conversion went through without errors:

exec("ffmpeg -i ".$orgVideoFile." -vcodec libx264 -crf 21 -acodec aac -ac 2 -ab 192000 -strict experimental -vpre hq -vpre veryfast -refs 3 -threads 4 -s 320x240 ".$newVideoFile, $output);
echo "Output:";
var_dump($output);

但是当检查$ output时,它总是空的:

but when checking $output its always empty:

Output:array(0) { } 

如何检查以确保一切正常?

How do I check to make sure everything went ok?

推荐答案

使用PHP的proc_open函数,您将拥有更多的控制权.它使您可以通过非常受控的方式来写入/读取标准输入/输出:

You will have much more control using PHP's proc_open function. It allows you to write/read standard input/output in a very controlled way:

$tunnels=array(
    0 => array('pipe','r'), // Process std input
    1 => array('pipe','w'), // Process std output
    2 => array('pipe','w') // Process std error
    );

$io=array();                      
$resource=proc_open("command with parameters...etc",$tunnels,$io);

if(!is_resource($resource)) {                       
    // Throw exception or something...
}

// Write to process standard input and close stream
fwrite($io[0],"Some data...");
fclose($io[0]);

// We are not interested in process standard output.. close it
fclose($io[1]);               

// Get process std error
$errors=stream_get_contents($io[2]);
fclose($io[2]);

// Close process reousrce
$result=proc_close($resource);

if($result != 0) {          
    // There where errors. Grab the error string from $errors
}         

这篇关于检查php exec的输出总是返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆