Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- echo -e "Usage: videosoundpost <video>"
- video=$1
- videofilename=${video##*'/'}
- beforedot=${videofilename%%'.'*}
- afterdot=${videofilename##*'.'}
- sound="/tmp/sound.mp3"
- muteoriginal="/tmp/mute.$afterdot"
- mutewebm="/tmp/video.webm"
- echo -e "Extracting audio..."
- ffmpeg -hide_banner -loglevel warning -stats -i "$video" $sound
- echo -e "Uploading audio to catbox.moe"
- HOST="https://catbox.moe/user/api.php"
- link=$(curl -F "reqtype=fileupload" -F "fileToUpload=@$sound" $HOST)
- echo -e "Uploaded sound to: $link"
- ffmpeg -hide_banner -loglevel warning -i "$video" -c copy -an "$muteoriginal"
- O_SIZE=$(echo "$(du -k --apparent-size "$muteoriginal" | cut -d ' ' -f 1) 1024" | awk '{print $1 / $2}')
- T_SIZE="3.8" # target size in MB
- if (( $(echo "$O_SIZE > $T_SIZE" |bc -l) )); then
- echo "original video larger than target size"
- else
- echo "original video smaller than target size"
- T_SIZE=$O_SIZE
- fi
- # Original duration in seconds
- O_DUR=$(\
- ffprobe \
- -v error \
- -show_entries format=duration \
- -of csv=p=0 "$muteoriginal")
- # Equals 1 if target size is ok, 0 otherwise
- IS_MINSIZE=$(\
- awk \
- -v size="$T_SIZE" \
- -v minsize="$T_MINSIZE" \
- 'BEGIN { print (minsize < size) }')
- # Give useful information if size is too small
- if [[ $IS_MINSIZE -eq 0 ]]; then
- printf "%s\n" "Target size ${T_SIZE}MB is too small!" >&2
- printf "%s %s\n" "Try values larger than" "${T_MINSIZE}MB" >&2
- exit 1
- fi
- # Calculate target video rate - MB -> KiB/s
- T_VRATE=$(\
- awk \
- -v size="$T_SIZE" \
- -v duration="$O_DUR" \
- -v audio_rate="0" \
- 'BEGIN { print ( ( size * 8192.0 ) / ( 1.048576 * duration ) - audio_rate) }')
- PROBE=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$muteoriginal")
- ORES=${PROBE##*'x'}
- TRES=480
- if [ "$ORES" -ge $TRES ]; then
- echo "Downscaling video to 480p"
- else
- TRES=$ORES
- fi
- # Perform the conversion
- echo "Target resolution is: '$TRES'p"
- echo "Target bitrate is: '$T_VRATE'K"
- echo -e "Converting video to .webm"
- ffmpeg -hide_banner -loglevel warning -stats -i "$muteoriginal" -vf scale=-1:"$TRES" -framerate 30 -c:v libvpx -b:v "$T_VRATE"K "$mutewebm"
- encode=$(echo "$link" | tail -c +9 | sed 's/:/%3A/g' | tr "/" "-" | sed "s/-/%2F/g")
- final=${beforedot}[sound=$encode].webm
- echo -e "$final"
- # Add filename to clipboard
- echo -n "$final" | xclip -selection clipboard
- # Copy video with the appropriate filename
- echo -e "Copying video..."
- cp "$mutewebm" "$final"
- # Clean up
- rm "$sound"
- rm "$muteoriginal"
- rm "$mutewebm"
Add Comment
Please, Sign In to add comment