1

该程序youtube-dl本身支持文件名中的非 ASCII 字符,它在我的网络服务器上以 root 用户和 www-data 用户完美运行,但是当我尝试使用 youtube-dl 和 PHP 下载视频时,非 ASCII 字符被完全跳过。

例如:Stromae - bâtard将被保存为Stromae - btard.mp4البث الحي作为.mp4

我正在使用此代码运行 CLI 命令

function cmd($string) {
  $descriptorspec = array(
     0 => array("pipe", "r"),  // stdin
     1 => array("pipe", "w"),  // stdout
     2 => array("pipe", "w"),  // stderr
  );
  $process = proc_open($string, $descriptorspec, $pipes);
  $stdout = stream_get_contents($pipes[1]);
  fclose($pipes[1]);
  $stderr = stream_get_contents($pipes[2]);
  fclose($pipes[2]);
  $ret = proc_close($process);
  return $stdout;
  }
$value = ('youtube-dl https://some.valid/link');
echo cmd($value);

请告诉我应该怎么做才能解决这个问题。

4

2 回答 2

2

检查你的 phpinfo(); LC_ALL 或 LC_LANG 设置的输出。我怀疑它与 PHP 无关,而是与您使用的 shell 环境与您的 Web 服务器正在使用的 shell 环境有关。

$value = ('LC_ALL=en_US.UTF-8 youtube-dl https://some.valid/link');
echo cmd($value);
于 2015-05-20T18:38:02.470 回答
1

默认情况下 PHP 使用 ISO-8859-1 字符集。将 PHP 配置为使用 UTF-8。您可以通过添加

mb_internal_encoding("UTF-8");

在脚本的开头

于 2015-05-20T16:39:41.217 回答