#!/bin/bash

version="1.0b-1"

echo "VidExtract $version, by Chir0student"
echo "Extract the audio from ANY video file and save it as an m4a (Apple Lossless Audio Codec)!!!"
echo


if [ "$UID" != 0 ]
then
	echo "Error: You must be root to run this script!"
	exit
fi

if [ -d != /var/mobile/Media/My\ Music/VidExtract ];
then
	mkdir -p /var/mobile/Media/My\ Music/VidExtract
else
	echo "Great! It looks like you've done this before!"
fi

if [ "$2" != "" ];
then
	orig=$1
	new=$2
else
	echo "Use:"
	echo "VidExtract </path/to/file.mp4> <new name> [start time] [length in secs]"
	echo
	echo "Example:"
	echo "VidExtract /private/var/mobile/YouTube\ Video.mp4 Now\ My\ Audio\ File 0 40"
	echo "VidExtract \"/private/var/mobile/YouTube Video\" \"Now\ My\ Audio\ File\""
	echo
	echo "PS: Your audio file will be saved in /var/mobile/Media/My\ Music/VidExtract"
	echo "Processing your request, the converting process takes a few seconds!"
	exit
fi

echo "Processing, wait..."

if [ "$4" != "" ]; then
	ffmpeg -i "$orig" -vn -ac 2 -ar 48000 -sample_fmt s16 -acodec alac -ss "$3" -t "$4" -y "$new".m4a >/dev/null 2>&1
	    elif [ "$3" != "" ]; then
		ffmpeg -i "$orig" -vn -ac 2 -ar 48000 -sample_fmt s16 -acodec alac -ss 0 -t "$3" -y "$new".m4a >/dev/null 2>&1
else
	ffmpeg -i "$orig" -vn -ac 2 -ar 48000 -sample_fmt s16 -acodec alac -y "$new".m4a >/dev/null 2>&1
fi

chmod 755 "$new".m4a
chown mobile "$new".m4a
chgrp mobile "$new".m4a
mv "$new".m4a /var/mobile/Media/My\ Music/VidExtract/"$new".m4a

echo
echo "Done. You can now find $new in your Pwntunes folder (My Music/VidExtract)"

exit 0
