Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # 01/14/11
- # ./autopad_spaces.sh /full/path/to/directory
- # ./autopad_spaces.sh /full/path/to/media
- # ./autopad_spaces.sh ./relative/path/to/media
- #A playlist.txt file is created where the command is run from (will be changed in next version)
- #playlist.txt file can be edited manually with a text editor or anything
- #Make sure you are "priveleged" enough to write symlinks in the directory from which you are # running this script
- #
- #To-do
- #add ustream compatibility
- #test huge playlists and possibly code to compensate
- #possibly move the ffmpeg to a subshell so that the main shell can be used by the user to view and edit the playlist or something
- #possibly add extra variables to compensate for special options to be passed directly to ffmpeg
- #possibly write a test to check for subtitles and autoselect english when an option is passed
- ##########-=DEFAULTS=-#########
- channel="kingdomheartsforever"
- username=""
- password=""
- x264Preset=$2
- if [ "$x264Preset" = "" ]; then
- x264Preset="faster"
- fi
- ################################
- #########-=Functions and Declarations=-##########
- #Declare an array for the playlist and initialize the playcount variable
- declare -a playlist
- playcount="0"
- #Function to initially generate the playlist when command is called with a directory
- function listGen
- {
- echo "generating playlist"
- ls -1 "$moviePath" | grep -i -e "\.avi$" -e "\.mkv$" -e "\.mpeg$" -e "\.mpg$" -e "\.mov$" > playlist.txt
- }
- #Function to refresh the playlist between playback from whatever is in the textfile. Allows for the user to manually edit playlist.txt and add or remove files
- function listRefresh
- {
- #Initialize entry variable and playlist array
- entry="0"
- unset playlist
- #Loop to sequentially populate array line by line from the playlist.txt file
- while read lolname; do
- playlist[$entry]=$lolname
- # echo $entry": "${playlist[$entry]}
- let entry++
- done < playlist.txt
- echo "Currently Playing File: "$playcount"/"${#playlist[@]}
- # echo ${playlist[@]}
- }
- #################################################
- ##########-=Input File Analysis=-###########
- moviePath=$1
- #Test if input is a normal file
- if [ -f "$moviePath" ]; then
- echo "Reading moviePath as ${moviePath##*.} file"
- #Test if file is a textfile and if so overwrite playlist.txt
- if [ ${moviePath##*.} = "txt" ]; then
- echo "loading txt file as playlist"
- cat $moviePath > playlist.txt
- else
- #If file is assumed to be a normal video file normalize the data so that the listRefresh function can also
- #be used normally. That way a user can launch the script with one file but then add to the playlist manually
- echo ${moviePath##*/} > playlist.txt
- moviePath=$(echo ${moviePath%/*})
- fi
- #Test if input is a directory
- elif [ -d "$moviePath" ]; then
- echo "Reading moviePath as directory"
- #Call playlist generation function
- listGen
- else
- #Input failed tests for file and directory
- echo "$moviePath not identified as either file or directory"
- exit
- fi
- #Normalize moviePath variable just to be safe
- moviePath=$(echo "$moviePath/")
- #Initialize playlist array from playlist.txt file
- listRefresh
- ############################################
- ##############-=Individual File Playback=-###############
- #Main loop for stream playback
- #Checks if the amount of files played is less than the amount of files loaded into the playlist array
- while [ $playcount -lt ${#playlist[@]} ] ; do
- #Creates a temp file to pull video width and height from while being useful for debugging
- ffprobe -show_streams "$moviePath${playlist[$playcount]}" > /tmp/probetmp.txt
- width=$(cat /tmp/probetmp.txt | grep width | sed 's/width=//')
- height=$(cat /tmp/probetmp.txt | grep height | sed 's/height=//')
- #Special test for checking of the video is widescreen to send it to the server in that way
- #Can be commented out if not necessary or problematic
- #Do not comment out nested tests
- if [ "$(echo "scale=0; (1000*$width*9)/($height*16)" | bc)" -ge "1000" ];then
- mode="widescreen" #variable for debugging
- scaleFactor=$(echo "scale=0; $width*9/16" | bc)
- padTotal=$(echo "scale=0; $scaleFactor - $height" | bc)
- padPixel=$(echo "scale=0; $padTotal/2" | bc)
- #Two different formats for setting the padding, switch if problematic
- #padString=" -padtop "$padPixel" -padbottom "$padPixel
- padString=" -vf pad="$width":"$scaleFactor":0:"$padPixel":black"
- else
- #Test for checking if the video needs padding on the top and bottom
- if [ "$(echo "scale=0; $width*3/4" | bc)" -gt "$height" ]; then
- mode="letterbox" #variable for debugging
- scaleFactor=$(echo "scale=0; $width*3/4" | bc)
- padTotal=$(echo "scale=0; $scaleFactor - $height" | bc)
- padPixel=$(echo "scale=0; $padTotal/2" | bc)
- #Two different formats for setting the padding, switch if problematic
- #padString=" -padtop "$padPixel" -padbottom "$padPixel
- padString=" -vf pad="$width":"$scaleFactor":0:"$padPixel":black"
- else
- #Padding to be added on the left and right if all other tests failed
- mode="tallscreen" #variable for debugging
- scaleFactor=$(echo "scale=0; $height*4/3" | bc)
- padTotal=$(echo "scale=0; $scaleFactor - $width" | bc)
- padPixel=$(echo "scale=0; $padTotal/2" | bc)
- #Two different formats for setting the padding, switch if problematic
- #padString="-padleft "$padPixel" -padright "$padPixel
- padString=" -vf pad="$scaleFactor":"$height":"$padPixel":0:black"
- fi
- fi
- #A bunch of output for debugging and whatnot
- echo "width: $width"
- echo "height: $height"
- echo "scaleFactor: $scaleFactor"
- echo "mode: $mode"
- echo "Widescreen test number: $(echo "scale=0; (1000*$width*9)/($height*16)" | bc)"
- echo "padTotal: $padTotal"
- echo "padPixel: $padPixel"
- echo "x264Preset: $x264Preset"
- echo "*****************************************"
- echo "PLAYING FILE ${playlist[$playcount]}"
- echo "*****************************************"
- # echo $moviePath
- #To maybe help with server side stream detection which sometimes fails with quick disconnect/reconnects
- sleep 5
- #ffmpeg command
- ffmpeg -i "$moviePath${playlist[$playcount]}" -re -acodec libmp3lame -ar 44100 -ac 2 -vcodec libx264 -vpre $x264Preset -f flv $padString -threads 0 "rtmp://fme.mogulus.com/mogulus/$channel/username=$username/password=$password/isAutoLive=true/autoVOD=true/autoRecord=false app=mogulus/$channel/username=$username/password=$password/isAutoLive=true/autoRecord=false tcurl=rtmp://fme.mogulus.com/mogulus/$channel/username=$username/password=$password/isAutolive=true/autoRecord=false swfUrl=rtmp://publish.livestream.com/mogulus/$channel/username=$username/password=$password/isAutolive=true/autoRecord=false flashver=FME/2.5\20(compatible;\20FMSc/0.9) live=true"
- let playcount++
- #Refresh the playlist array from the playlist.txt file after playback
- listRefresh
- done
- #########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement