1

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.

1 Answer 1

2

Let's look at an example: The element having id="eow-title" in the document at https://www.youtube.com/watch?v=FTXN5nOstRs looks like this:

  <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>

And since there's nothing telling libxml that leading/trailing whitespaces are irrelevant, the node value of that element is "{enter}{space}{space}{space}{space}Richard Dawkins...".

You might get rid of the whitespaces e.g. via trim().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.