Advertisement
teknoraver

convert

Jun 19th, 2023
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. src=$1
  4. dst=${1%.*}.mp4
  5. bak=${1%.*}.orig.${1##*.}
  6.  
  7. sqldb=convert.db
  8.  
  9. sqlinit() {
  10.     [ -f "$1" ] || sqlite3 "$1" 'CREATE TABLE encodings(path TEXT PRIMARY KEY NOT NULL, orig TEXT, backup TEXT, origsize INT, newsize INT)'
  11. }
  12.  
  13. restore() {
  14.     rm -f "$dst"
  15.     mv "$bak" "$src"
  16. }
  17.  
  18. sqlinit "$sqldb"
  19.  
  20. [ -f "$src" ] || exec echo "fail to open $dst"
  21. [ -f "$bak" ] && exec echo "backup $bak already exists"
  22.  
  23. exist=$(sqlite3 "$sqldb" "SELECT count(*) FROM encodings WHERE path = '$dst' OR orig = '$src'")
  24.  
  25. [ "$exist" -gt 0 ] && exec echo "$dst already converted"
  26.  
  27. mv "$src" "$bak"
  28.  
  29. echo "Converting \"$dst\""
  30. #if /usr/local/bin/ffmpeg -v 16 -y -vaapi_device /dev/dri/renderD128 -i "$bak" -vf format=nv12,hwupload -c:v h264_vaapi -c:a libfdk_aac -vbr 4 "$dst" && [ -s "$dst" ]; then
  31. #if ffmpeg -v 16 -y -vaapi_device /dev/dri/renderD128 -i "$bak" -vf format=nv12,hwupload -c:v h264_vaapi -c:a copy -qp 24 "$dst" && [ -s "$dst" ]; then
  32. if ffmpeg -v 16 -y -i "$bak" -crf 22 -preset slow -c:a copy "$dst" && [ -s "$dst" ]; then
  33.     origsize=$(stat "$bak" -c %s)
  34.     newsize=$(stat "$dst" -c %s)
  35.     save=$((newsize * 100 / origsize))
  36.  
  37.     echo "$((origsize >> 20)) => $((newsize >> 20)) MB, $save %"
  38.  
  39.     if [ $save -le 90 ]; then
  40.         sqlite3 "$sqldb" "INSERT INTO encodings VALUES('$dst', '$src', '$bak', $origsize, $newsize)"
  41.     else
  42.         echo 'restoring old file'
  43.         sqlite3 "$sqldb" "INSERT INTO encodings VALUES('$src', NULL, NULL, NULL, NULL)"
  44.         restore
  45.     fi
  46. else
  47.     echo 'error, restoring'
  48.     restore
  49. fi
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement