forked from DaCHack/SynoVideoGPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynoVideoGPS.sh
105 lines (82 loc) · 3.31 KB
/
SynoVideoGPS.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#set -x errexit
# SynoVideoGPS Version 0.1.2
mypass="$1" # provide user password as first parameter to the script
dir="." # script should be run from /volumeX/photo/ to scan the entire tree (it is not relevant where on the volume the script is located)
myuser=$USER # always use WebAPI as currently logged in user
ds_ip="localhost" # change to IP adress if localhost does not work
#Log into PS
login_token=$( curl -c cookies.txt "http://${ds_ip}/photo/webapi/auth.php?api=SYNO.PhotoStation.Auth&method=login&version=1&username=$myuser&password=$mypass" 2>&1 )
if [[ ! "$login_token" =~ .*sid.* ]]; then
echo "Login Error."
exit 1
fi
echo "Login successful. Cookie created..."
#Check if community version of ffmpeg installed
ffmpeg_tool="ffmpeg"
for disk in {1..8}; do
if test -f "/volume${disk}/@appstore/ffmpeg/bin/ffmpeg"; then
ffmpeg_tool="/volume${disk}/@appstore/ffmpeg/bin/ffmpeg"
fi
done
#Scan through subdirectories
counter=0
total=$( find "$dir" -print | grep -v "@eaDir" | wc -l )
find "$dir" -print0 | while IFS= read -r -d '' current_path
do
#Do nothing for Thumbnails etc.
if [[ $current_path = *"eaDir"* ]]; then
continue
fi
#Prepare progress info
((counter=counter+1))
((progress=(counter*100/total)))
#For all non-directories we can continue depending on file type
echo -e "[${progress}%]\tScanning $current_path"
file=$(basename -- "$current_path")
#Check MOV files from iPhones
if [ "${file##*.}" = "MOV" ]; then
geo_data=$( ${ffmpeg_tool} -i "${current_path}" -f ffmetadata 2>&1 | awk '/com.apple.quicktime.location.ISO6709/{print substr($2,1,length($2)-1)}' )
#Check MP4 files from GoPro
elif [ "${file##*.}" = "MP4" ]; then
geo_data=$( ${ffmpeg_tool} -i "${current_path}" -f ffmetadata 2>&1 | awk '/location /{print substr($3,1,length($3)-1)}' )
else
#Skip all other file types
continue
fi
# Extract GPS data from ffmpeg output
GPS_latitude=$( echo $geo_data | awk -F"+|-" '{print substr($0,index($0,$2-1),1) $2}' )
GPS_longitude=$( echo $geo_data | awk -F"+|-" '{print substr($0,index($0,$3-1),1) $3}' )
# URLencode GPS data
GPS_latitude=$( echo "$GPS_latitude" | sed 's/+/%2B/g' | sed 's/-/%2D/g' | sed 's/\./%2E/g' )
GPS_longitude=$( echo "$GPS_longitude" | sed 's/+/%2B/g' | sed 's/-/%2D/g' | sed 's/\./%2E/g' )
#Skip if no GPS data found
if [[ $GPS_latitude != *"%2E"* ]]; then
continue
fi
if [[ $GPS_longitude != *"%2E"* ]]; then
continue
fi
#Print GPS data if something is found
echo -n " ${geo_data} "
echo -n " ${GPS_latitude} "
echo -n " ${GPS_longitude} "
#Get video path (relative to /photo/) -> cut "/volumeX/photo/"
video_path=$(dirname "${current_path}" | cut -d'/' -f 4- )
#echo $video_path
#Build video ID
#video_<AlbumPathInHex>_<PhotoPathInHex>
album_SubID=$(echo -n "${video_path}/" | od -An -t x1 | tr -d '\n ')
video_SubID=$(echo -n "$file" | od -An -t x1 | tr -d '\n ')
video_id=$( echo "video_${album_SubID}_${video_SubID}")
#Save Location to video
url="http://${ds_ip}/photo/webapi/photo.php?api=SYNO.PhotoStation.Photo&method=edit&version=1&id=${video_id}&gps_lat=${GPS_latitude}&gps_lng=${GPS_longitude}"
edit_result_token=$( curl -b cookies.txt "$url" 2>&1)
if [[ ${edit_result_token} != *"true"* ]]; then
echo "Error: Could not save GPS data"
exit 1
fi
echo "GPS data saved"
done
#Clean up and remove cookies.txt
rm cookies.txt