据我所知,这一点以前已经得到了回答,但是代码中有一个奇怪的错误。
下面是bash代码,它从PHP中获取$title变量:
#!/bin/bash
url=$1
type=$2
bitrate=$3
vidid=$4
title=$TITLE
TMP_FILE="youtube-mp3-$RANDOM.tmp"
youtube-dl --get-title --get-url --get-filename "$url" > $TMP_FILE 2> "/tmp/$TMP_FILE.err"
exec 42< $TMP_FILE
while read video_title <&42 ; do
read video_url <&42
read video_filename <&42
if [ "$type" = "vorbis" ]; then
#youtube-dl -x --prefer-ffmpeg --audio-format vorbis --audio-quality "$bitrate" "$url"
echo "$title" > title-test
else
#youtube-dl -x --prefer-ffmpeg --audio-format "$type" --audio-quality "$bitrate" "$url"
echo "$title" > title-test
fi
done
exec 42<&-
rm -f $TMP_FILE下面是将变量发送到bash的PHP代码:
$link = $cmd;
$video_id = explode("?v=", $link);
$video_id = $video_id[1];
$yturl = "http://www.youtube.com/watch?v=".$video_id;
$page = file_get_contents($yturl);
$doc = new DOMDocument();
$doc->loadHTML($page);
$title_div = $doc->getElementById('eow-title');
$title = $title_div->nodeValue;
putenv("TITLE=$title");它确实正确地将YouTube视频标题回显为" title -test",但是当它这样做时,它会发送如下:{enter}{space}{space}$title。
所以在视频标题之前有空格,当我尝试使用bash中的mv、rm或cp $title变量时,它不能正确工作,我不知道为什么会发生这种情况。
发布于 2016-02-21 03:29:14
让我们看一个示例:在id=的文档中包含https://www.youtube.com/watch?v=FTXN5nOstRs的元素“eow-title”如下所示:
<span id="eow-title" class="watch-title " dir="ltr" title="Richard Dawkins exploding at bullshit in the Bible">
Richard Dawkins exploding at bullshit in the Bible
</span>而且,由于没有任何东西告诉libxml引导/尾随空格是不相关的,所以该元素的节点值是“{enter}{space}{space}Richard.”。
您可以去掉空白空间,例如通过修剪()。
https://stackoverflow.com/questions/35531888
复制相似问题