Guest User

videosoundpost

a guest
Jul 14th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo -e "Usage: videosoundpost <video>"
  4.  
  5. video=$1
  6. videofilename=${video##*'/'}
  7. beforedot=${videofilename%%'.'*}
  8. afterdot=${videofilename##*'.'}
  9. sound="/tmp/sound.mp3"
  10. muteoriginal="/tmp/mute.$afterdot"
  11. mutewebm="/tmp/video.webm"
  12.  
  13. echo -e "Extracting audio..."
  14. ffmpeg -hide_banner -loglevel warning -stats -i "$video" $sound
  15.  
  16. echo -e "Uploading audio to catbox.moe"
  17. HOST="https://catbox.moe/user/api.php"
  18. link=$(curl -F "reqtype=fileupload" -F "fileToUpload=@$sound" $HOST)
  19.  
  20. echo -e "Uploaded sound to: $link"
  21.  
  22.  
  23. ffmpeg -hide_banner -loglevel warning -i "$video" -c copy -an "$muteoriginal"
  24.  
  25. O_SIZE=$(echo "$(du -k --apparent-size "$muteoriginal" | cut -d ' ' -f 1) 1024" | awk '{print $1 / $2}')
  26. T_SIZE="3.8" # target size in MB
  27.  
  28. if (( $(echo "$O_SIZE > $T_SIZE" |bc -l) )); then
  29. echo "original video larger than target size"
  30. else
  31. echo "original video smaller than target size"
  32. T_SIZE=$O_SIZE
  33. fi
  34.  
  35. # Original duration in seconds
  36. O_DUR=$(\
  37. ffprobe \
  38. -v error \
  39. -show_entries format=duration \
  40. -of csv=p=0 "$muteoriginal")
  41.  
  42. # Equals 1 if target size is ok, 0 otherwise
  43. IS_MINSIZE=$(\
  44. awk \
  45. -v size="$T_SIZE" \
  46. -v minsize="$T_MINSIZE" \
  47. 'BEGIN { print (minsize < size) }')
  48.  
  49. # Give useful information if size is too small
  50. if [[ $IS_MINSIZE -eq 0 ]]; then
  51. printf "%s\n" "Target size ${T_SIZE}MB is too small!" >&2
  52. printf "%s %s\n" "Try values larger than" "${T_MINSIZE}MB" >&2
  53. exit 1
  54. fi
  55.  
  56. # Calculate target video rate - MB -> KiB/s
  57. T_VRATE=$(\
  58. awk \
  59. -v size="$T_SIZE" \
  60. -v duration="$O_DUR" \
  61. -v audio_rate="0" \
  62. 'BEGIN { print ( ( size * 8192.0 ) / ( 1.048576 * duration ) - audio_rate) }')
  63.  
  64. PROBE=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$muteoriginal")
  65. ORES=${PROBE##*'x'}
  66. TRES=480
  67.  
  68. if [ "$ORES" -ge $TRES ]; then
  69. echo "Downscaling video to 480p"
  70. else
  71. TRES=$ORES
  72. fi
  73.  
  74. # Perform the conversion
  75. echo "Target resolution is: '$TRES'p"
  76. echo "Target bitrate is: '$T_VRATE'K"
  77. echo -e "Converting video to .webm"
  78. ffmpeg -hide_banner -loglevel warning -stats -i "$muteoriginal" -vf scale=-1:"$TRES" -framerate 30 -c:v libvpx -b:v "$T_VRATE"K "$mutewebm"
  79.  
  80. encode=$(echo "$link" | tail -c +9 | sed 's/:/%3A/g' | tr "/" "-" | sed "s/-/%2F/g")
  81. final=${beforedot}[sound=$encode].webm
  82. echo -e "$final"
  83.  
  84. # Add filename to clipboard
  85. echo -n "$final" | xclip -selection clipboard
  86.  
  87. # Copy video with the appropriate filename
  88. echo -e "Copying video..."
  89. cp "$mutewebm" "$final"
  90.  
  91. # Clean up
  92. rm "$sound"
  93. rm "$muteoriginal"
  94. rm "$mutewebm"
Add Comment
Please, Sign In to add comment