I understand this has been answered before, however there's a weird bug in the code.
Here is the bash code which gets the $title variable from the PHP script:
#!/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
And here is the PHP code that sends the variable to bash:
$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");
It does correctly echo the YouTube video title to "title-test", however when it does it sends it like: {enter}{space}{space}{space}{space}$title
So there's blank spaces before the video title, which doesn't correctly work when I attempt to mv, rm or cp the $title variable in bash, and I don't know why this is happening.