Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get filename in a bash script #4870

Closed
augnustin opened this issue Feb 4, 2015 · 7 comments
Closed

Get filename in a bash script #4870

augnustin opened this issue Feb 4, 2015 · 7 comments

Comments

@augnustin
Copy link

@augnustin augnustin commented Feb 4, 2015

I want to automate sending the newly downloaded file to my google play music using the almost-as-awesome-as-youtube-dl-library http://unofficial-google-music-api.readthedocs.org/

I am aware I could do that in python, but not being a python dev, I'd rather write a bash script for higher level operation.

Here's my take

download_music(){
  cd ~/Music;
  file = youtube-dl $1 --default-search "ytsearch";
  echo $file;
PYTHON_ARG="$file" python <<END
from gmusicapi import Musicmanager
import os
mm = Musicmanager()
mm.login()
mm.upload(os.environ['PYTHON_ARG'])
END
}

My single trouble is to get the filename (or filepath) generated from youtube-dl script. Any tip on how I could do that?

I'll post final solution for those who could be interested.

Thanks a lot,

@globau
Copy link

@globau globau commented Feb 15, 2015

--get-filename - simulate, quiet but print output filename

@augnustin
Copy link
Author

@augnustin augnustin commented Feb 16, 2015

It won't work for me. It the file downloaded is a youtube video, the output is the mp4 file and not the postprocessed mp3 file (because of -x option).

I ended up creating a temporary folder, downloading into it, lsing it, uploading it and aksing wether or not I want to delete the file.

Here's the code for those interested:

upload_music() {
  PYTHON_ARG="$1" python <<END
from gmusicapi import Musicmanager
import os
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
mm = Musicmanager(False, False, False)
mm.login()
mm.upload(os.environ['PYTHON_ARG'])
END
}

ask_remove() {
  echo "Do you want to keep $1?"
  select yn in "Yes" "No"; do
      case $yn in
          [Yy]* ) mkdir -p mv $1 ~/Musique/ break;;
          [Nn]* ) exit;;
      esac
  done
}

download_music(){
  cd ~/Musique;
  mkdir -p tmp;
  cd tmp;
  youtube-dl --default-search "ytsearch" -x --audio-format mp3 --no-mtime --prefer-ffmpeg $1;
  file="$(ls -t | head -n 1)";
  echo "$file downloaded";
  upload_music $file;
  echo "$file uploaded";
  ask_remove $file;
  cd ..;
  rm -rf tmp;
}

@williamboman
Copy link

@williamboman williamboman commented Mar 16, 2015

I'm trying to do the almost exact same thing.

What I've come up with is something like this;

youtube-mp3() {
    local tmp_dir="/tmp/youtube-dl";
    local filename=$(youtube-dl "$1" -o "%(title)s" --get-filename);

    mkdir -p "$tmp_dir";
    pushd "$tmp_dir" > /dev/null;
    youtube-dl "$1" -x -o "%(title)s.%(ext)s" --audio-format=mp3 --audio-quality=512k && mv "${filename}.mp3" "${HOME}/Drive/Music";
    popd > /dev/null;
}

There has to be a better way of doing this? I can't seem to find a solution in the manual.

@yan12125
Copy link
Collaborator

@yan12125 yan12125 commented Mar 16, 2015

The output template accept path parts. For example,

youtube-dl "$1" -x -o "~/Drive/Music/%(title)s.mp3" --audio-format=mp3 --audio-quality=512k
@williamboman
Copy link

@williamboman williamboman commented Mar 16, 2015

@yan12125 Yeah that's how I did it before. But when you directly set the file to .mp3 it will skip the actual mp3 post-processing (afaik). The reason I want to initially download it to /tmp is to avoid syncing the .part and i.e. .m4a files that are created during download with Google Drive.

I think I managed to update my script after you commented, it works perfectly now.

@linuxdv
Copy link

@linuxdv linuxdv commented Jun 7, 2015

@williamboman i am have similar issue - when download mp3 and set option -o file.mp3 youtube-dl skip post-processing and resulting file is not readable in podcast players.
i set option -o file.data* and youtube-dl download file to .data and convert to .mp3 file

@yan12125
Copy link
Collaborator

@yan12125 yan12125 commented Nov 28, 2017

The remaining issue is that --get-filename doesn't work with post-processing (mp3 conversion, etc.). This is discussed in #7137.

@yan12125 yan12125 closed this Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.