teknoraver icon

convert

teknoraver | PRO | 06/19/23 08:36:54 PM UTC | 0 ⭐ | 2346 👁️ | Never ⏰ | []
Bash |

1.48 KB

|

None

|

0 👍

/

0 👎

#!/bin/sh
 
src=$1
dst=${1%.*}.mp4
bak=${1%.*}.orig.${1##*.}
 
sqldb=convert.db
 
sqlinit() {
    [ -f "$1" ] || sqlite3 "$1" 'CREATE TABLE encodings(path TEXT PRIMARY KEY NOT NULL, orig TEXT, backup TEXT, origsize INT, newsize INT)'
}
 
restore() {
    rm -f "$dst"
    mv "$bak" "$src"
}
 
sqlinit "$sqldb"
 
[ -f "$src" ] || exec echo "fail to open $dst"
[ -f "$bak" ] && exec echo "backup $bak already exists"
 
exist=$(sqlite3 "$sqldb" "SELECT count(*) FROM encodings WHERE path = '$dst' OR orig = '$src'")
 
[ "$exist" -gt 0 ] && exec echo "$dst already converted" 
 
mv "$src" "$bak"
 
echo "Converting \"$dst\""
#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
#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
if ffmpeg -v 16 -y -i "$bak" -crf 22 -preset slow -c:a copy "$dst" && [ -s "$dst" ]; then
    origsize=$(stat "$bak" -c %s)
    newsize=$(stat "$dst" -c %s)
    save=$((newsize * 100 / origsize))
 
    echo "$((origsize >> 20)) => $((newsize >> 20)) MB, $save %"
 
    if [ $save -le 90 ]; then
        sqlite3 "$sqldb" "INSERT INTO encodings VALUES('$dst', '$src', '$bak', $origsize, $newsize)"
    else
        echo 'restoring old file'
        sqlite3 "$sqldb" "INSERT INTO encodings VALUES('$src', NULL, NULL, NULL, NULL)"
        restore
    fi
else
    echo 'error, restoring'
    restore
fi
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎