Skip to content

Instantly share code, notes, and snippets.

@grigoroiualex
Forked from L0rdCha0s/convert_videos.sh
Created August 5, 2022 15:40
Show Gist options
  • Save grigoroiualex/1ece99a13b110c72030b91ac60ff8f08 to your computer and use it in GitHub Desktop.
Save grigoroiualex/1ece99a13b110c72030b91ac60ff8f08 to your computer and use it in GitHub Desktop.
IFS=$'\n'
myUser=`whoami`
echo Local user is $myUser
SYNO_USER=`psql synofoto -t -c "select id from user_info where name='$myUser'"`
echo SYNO_USER is $SYNO_USER
echo Scanning for video files..
for i in `find . -iname \*.mov -type f | grep -v eaDir | grep -v "_3"`; do
#echo i is $i
fname=$(dirname ${i})/@eaDir/$(basename ${i})/SYNOPHOTO_FILM_H.mp4;
#echo Fname: $fname
foldername=$(dirname $i)
trimmedFolderName="${foldername#?}"
#echo Foldername: $foldername
filename=$(basename $i)
#echo Filename: $filename
#echo psql synofoto -t -c "select video.id from video join unit on unit.id = video.id where unit.id_folder = (select id from folder where name='$trimmedFolderName' and id_user=$SYNO_USER) and unit.filename='$filename'"
video_id=`psql synofoto -t -c "select video.id from video join unit on unit.id = video.id where unit.id_folder = (select id from folder where name='$trimmedFolderName' and id_user=$SYNO_USER) and unit.filename='$filename'"`
#echo Video ID: $video_id
video_convert_present=`psql synofoto -t -c "select id_unit from video_convert where id_unit=$video_id"`
if [ -z "$video_convert_present" ]; then
if [ -f $fname ]; then
echo \*\*\*\*\*\* Preview already exists \($fname\);
else
echo $fname does not exist, making video at $fname
/volume1/@appstore/CodecPack/bin/ffmpeg41 -i $i -c:v libx264 -b:v 12M -bufsize 1M -maxrate 12M -crf 18 -vf format=yuv420p -c:a copy $fname &> /dev/null
CONV_RES=$?
fi;
fileSize=`wc -c $fname | awk '{ print $1 }'`
if [ $fileSize -gt 0 ]; then
#Get duration
duration=`ffmpeg -i $fname 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'`
duration=$(( 1000*duration ))
echo Duration is $duration
#Get width and height...
dimensions=`ffmpeg -i $fname 2>&1 | grep Stream | grep Video | awk -F"," '{ print $3 }'`
width=`echo $dimensions | awk -F "x" '{ print $1 }'`
height=`echo $dimensions | awk -F "x" '{ print $2 }'`
echo Width: $width, Height: $height
#Now make psql entry...
echo Video ID in DB is: $video_id , inserting recording into database...
if [ -z $video_convert_present ]; then
echo "insert into video_convert(id_unit, duration, quality,video_info,audio_info) select $video_id, $duration, 'high', '{\"container_type\":\"mp4\",\"frame_bitrate\":7832773,\"frame_rate_den\":1,\"frame_rate_num\":30,\"orientation\":1,\"resolution_x\":$width,\"resolution_y\":$height,\"video_bitrate\":7726253,\"video_codec\":\"h264\",\"video_level\":31,\"video_profile\":3}', '{\"audio_bitrate\":126398,\"audio_codec\":\"aac_lc\",\"channel\":2,\"frequency\":44100}'" | psql synofoto
else
echo "Video convert record already exists.."
fi;
else
echo Could not convert video, bailing out for $filename ..
fi
else
echo Video already converted: $filename
fi
done