Skip to content

Instantly share code, notes, and snippets.

@L0rdCha0s
Last active March 28, 2024 17:12
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save L0rdCha0s/2a3ad183ede4b5477149322cca907841 to your computer and use it in GitHub Desktop.
Save L0rdCha0s/2a3ad183ede4b5477149322cca907841 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
@pkulak
Copy link

pkulak commented Dec 27, 2021

This is so great, thank you!

Couple notes I had for when I need to do this again and forget everything.

  • I had to add | grep -v " " on line 3 because I have a bunch of garbage file names from 15 years ago that were screwing things up.
  • Android phones are using HEVC now! What the hell? Anyway, changing 29 and 30 to awk -F "[x ]" (use space as a separator as well) fixes a little issue there (if you search for mp4s instead of movs).
  • I needed to go nuclear on my user psql permissions: ALTER USER xx WITH SUPERUSER;

Anyway, this worked like a damn charm. I hope Synology fixes this soon (why run the conversion for new but not old?), but until then, this is what you need. Anyay, thanks again!

EDIT: Oh, and I had to do this, on line 5, since I put my Google imports into their own folder:

foldername=$(dirname $i | cut -c 2-)

@raveensrk
Copy link

Hi what does this script do?

@cantpitch
Copy link

Hi what does this script do?

It converts HEVC files that were copied into your photos directory rather than imported through the app.

@isLaLa
Copy link

isLaLa commented Nov 6, 2022

Can you write a detailed operation tutorial, I won't use it, thank you

@alirz1
Copy link

alirz1 commented Nov 24, 2022

Does this still work on DSM 7.1.x ? I have quite a few copied over HEVC videos that dont show thumbnails in the Photos app. I think this is what i might need.
Question, does this convert the videos to another format or just helps the photos app process and create the thumbnails for HEVC files? Thanks

EDIT: should have read the code first before posting i guess. So looks like it is converting the vidos. Anyway of simply triggering photos to create thumbnails for hevc files instead?

@alirz1
Copy link

alirz1 commented Nov 28, 2022

Well ive done everything, even though the script runs, it doesnt seem to be doing anything, HEVC thumbnails are still not visible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment