Script to cache my YouTube playlist to oPlayer

Just have a drink and chat.

Script to cache my YouTube playlist to oPlayer

Postby lascenso » Thu Aug 22, 2013 3:19 pm

Hello all!

I have a peculiar need. Maybe not so much of you match this need, any way here is my contribute.
I use YouTube playlist as my MAIN playlist so it's available everywhere OFFLINE (at work, home, Apple TV, iPhone, tablet, car, blah...) and as video or music only playlist (iPhone native video APP does not allow you to run videos in background for those situations when we just want to listening to music, that's why oPlayer is amazing).
YouTube playlist allow me to add musics anywhere (using a PC at work or using a tablet or iPhone and SoundHound or Shazam anywhere, etc). It's easy, quick and widely available. Also allow me to use it as music only playlist to listening to while working or at the gymn but also to play the movies when dinning with friends at home using Apple TV. Better multimedia experience than listening to music only. YouTube is amazing, it's a fact.

Also I don't like (therefore don't use) iTunes to transfer multimedia files.

Recently Google updated YouTube rules and does not allow to save videos on cache anymore so all mobile APPS which build cache to allow YouTube playlists to play while offline (those apps which do everything I need) are removing cache feature, which means, they don't work offline anymore. Using 3G/LTE/Wifi each time we want to watch movies and/or listening to music consumes lot's of battery and traffic, so this is not a solution anymore.

My script contribution and oPlayer do everything I need, automatically, so I can listening only or watch and listening YouTube videos offline anywhere:
1. download all videos from my YouTube playlist
2. convert those FLV to MP4 (iPhone compatible) without loosing quality or file size incrementation
3. normalise sound for all video files
4. upload them to iPhone oPlayer directory using it's FTP server feature

It's a bash script so it runs on linux machines only. Mine is an Ubuntu 12.04.
You need ffmpeg/avconv, normalize-audio and ncftp installed (sudo apt-get install ffmpeg normalize-audio ncftp).
If you want to upload files to your iPhone too:
1. script PC and iPhone must be connected at same WiFi network.
2. oPlayer must be on "WiFi Transfer" mode (here you can find your iPhone IP to set on script SETTINGS section)
Feel free to change it if you want and use it on your own risk.

Cheers!

(create a temporary directory, create a file with below content, change SETTINGS section and run it)
Code: Select all
#!/bin/bash
################################
#
# Syntax:
# update [option]
#
# Options:
# download (only download video(s) from YouTube)
# convert (only convert existent FLV videos to MP4 videos)
# normalise (only normalise audio from existent videos)
# upload (only upload video files to iPhone)
# clean (delete all files created by this script - only original YouTube video files will remains)
#
################################
#  SETTINGS                    #
################################
#
# YouTube settings
YOUTUBEURL="http://www.youtube.com/playlist?list=PLoK-n94kK1uqe8vq9Zv2-6HIQcLGsG-V9" # use single video or playlist URL (if playlist used all videos will be processed)

# iPhone FTP connection settings (you must run oPlayer WiFi Transfer - bellow data can be checked here)
IPHONEFTPIP="192.168.9.90" # iPhone network IP
IPHONEFTPPORT=2121 # iPhone FTP port
IPHONEFTPDIR="youtube/" # if you want to use specific path, otherwise use "./" (if you use "/" without . (dot) upload will fail) - if path does not exist, it will be created

# Video and sound quality
AUDIOGAIN="5Db" # use "0Db" to avoid audio gain

################################
#  SCRIPT CODE                 #
################################
#
# Declare functions

# Download videos from YouTube
function download {
  echo "Downloading video(s) from $YOUTUBEURL..."
  # PROCEDURE #1
  # download everything using format FLV 480p H.264
  # small files with average quality
  # every FLV will be converted to MP4 mantaining quality (and therefore file size)
  #youtube-dl -f 35 -o '%(title)s.%(ext)s' $YOUTUBEURL
  #
  # PROCEDURE #2
  # download on available main format
  # most of the files will be fetched in mp4 but with huge sizes
  # FLV's will also be converted to MP4 mantaining quality (and therefore file size)
  # don't know if this is better procedure than procedure #1 - maybe this fetch files on maximum quality available
  youtube-dl -i -c -o '%(title)s.%(ext)s' $YOUTUBEURL
}

# Convert all downloaded FLV to MP4 preserving same quality (and therefore file size)
function convert {
  echo "Converting FLV's to MP4's..."
  for file in *.flv
  do
    echo "Converting '$file' to '$file.mp4'..."
    #ffmpeg -i "$file" -c:v libx264 -crf 23 -c:a libfaac -q:a 100 "$file.mp4"
    #ffmpeg -i "$file" -sameq -ar 22050 "$file.mp4"
    avconv -y -i "$file" -codec copy "$file.mp4"
  done
}

# Normalise audio for all downloaded videos
function normalise {
  for file in *.mp4
  do
    echo "Normalizing audio for '$file'..."
    VIDEO_FILE=$file
    VIDEO_FILE_FIXED=${VIDEO_FILE%.*}-fixed.${VIDEO_FILE##*.}
    avconv -y -i "$VIDEO_FILE" -c:a pcm_s16le -vn audio.wav
    normalize-audio -g $AUDIOGAIN audio.wav
    avconv -y -i "$VIDEO_FILE" -i audio.wav -map 0:0 -map 1:0 -c:v copy -c:a libvo_aacenc "$VIDEO_FILE_FIXED"
  done
}

# Upload files to iPhone using oPlayer FTP server
function upload {
  for file in *-fixed.mp4
  do
    echo "Uploading to iPhone: '$file' => '$IPHONEFTPDIR$file'"
    ncftpput -m -P $IPHONEFTPPORT $IPHONEFTPIP $IPHONEFTPDIR "$file"
  done
}

# Clean cache (only original downloaded videos from YouTube will remains)
function clean {
  echo "Cleaning script generated files..."
  rm -fr *.flv.mp4 *-fixed.mp4 audio.wav
}

# End script with message
function bye {
  echo "
Script finished!
"
  exit
}

# Detect options
if [[ "$1" == "download" ]]
  then download
  bye
elif [[ "$1" == "convert" ]]
  then convert
  bye
elif [[ "$1" == "normalise" ]]
  then normalise
  bye
elif [[ "$1" == "upload" ]]
  then upload
  bye
elif [[ "$1" == "clean" ]]
  then clean
  bye
fi

# Execute everything (no options)
download
convert
normalise
upload
clean
bye
lascenso
Beignner
Beignner
 
Posts: 3
Joined: Thu Aug 22, 2013 2:03 pm

Return to Coffee Corner

Who is online

Users browsing this forum: No registered users and 15 guests