1 | <?php |
---|
2 | /*********************************************** |
---|
3 | * File : function_sync.php |
---|
4 | * Project : piwigo-videojs |
---|
5 | * Descr : Generate the admin panel |
---|
6 | * |
---|
7 | * Created : 9.06.2013 |
---|
8 | * |
---|
9 | * Copyright 2012-2013 <xbgmsharp@gmail.com> |
---|
10 | * |
---|
11 | * |
---|
12 | * This program is free software: you can redistribute it and/or modify |
---|
13 | * it under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation, either version 3 of the License, or |
---|
15 | * (at your option) any later version. |
---|
16 | * |
---|
17 | * This program is distributed in the hope that it will be useful, |
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | * GNU General Public License for more details. |
---|
21 | * |
---|
22 | * You should have received a copy of the GNU General Public License |
---|
23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | * |
---|
25 | ************************************************/ |
---|
26 | |
---|
27 | // Check whether we are indeed included by Piwigo. |
---|
28 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
29 | |
---|
30 | include_once("function_frame.php"); |
---|
31 | |
---|
32 | /*************** |
---|
33 | * |
---|
34 | * Start the sync work |
---|
35 | * |
---|
36 | */ |
---|
37 | |
---|
38 | // Check the presence of the DB schema |
---|
39 | $sync_options['sync_gps'] = true; |
---|
40 | $q = 'SELECT COUNT(*) as nb FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = "'.IMAGES_TABLE.'" AND COLUMN_NAME = "lat" OR COLUMN_NAME = "lon"'; |
---|
41 | $result = pwg_db_fetch_array( pwg_query($q) ); |
---|
42 | if($result['nb'] != 2) |
---|
43 | { |
---|
44 | $sync_options['sync_gps'] = false; |
---|
45 | } |
---|
46 | |
---|
47 | // Init value for result table |
---|
48 | $videos = 0; |
---|
49 | $metadata = 0; |
---|
50 | $thumbs = 0; |
---|
51 | $errors = array(); |
---|
52 | $infos = array(); |
---|
53 | |
---|
54 | if (!$sync_options['sync_gps']) |
---|
55 | { |
---|
56 | $errors[] = "latitude and longitude disable because the require plugin is not present, eg: 'OpenStreetMap'."; |
---|
57 | } |
---|
58 | |
---|
59 | /* |
---|
60 | if ($sync_options['thumb']) |
---|
61 | $log = system("ffmpeg -version", $retval); |
---|
62 | if (($retval != 0) and $sync_options['thumb']) |
---|
63 | { |
---|
64 | $errors[] = "Thumbnail creation disable because ffmpeg is not installed on the system, eg: '/usr/bin/ffmpeg'."; |
---|
65 | $sync_options['thumb'] = false; |
---|
66 | } |
---|
67 | } |
---|
68 | */ |
---|
69 | |
---|
70 | if (!$sync_options['metadata'] and !$sync_options['thumb']) |
---|
71 | { |
---|
72 | $errors[] = "You ask me to do nothing, are you sure?"; |
---|
73 | } |
---|
74 | |
---|
75 | // Avoid Conflict with other plugin using getID3 |
---|
76 | if( !class_exists('getID3')){ |
---|
77 | // Get video infos with getID3 lib |
---|
78 | require_once(dirname(__FILE__) . '/../include/getid3/getid3.php'); |
---|
79 | } |
---|
80 | $getID3 = new getID3; |
---|
81 | // Do the job |
---|
82 | $result = pwg_query($query); |
---|
83 | while ($row = pwg_db_fetch_assoc($result)) |
---|
84 | { |
---|
85 | //print_r($row); |
---|
86 | $filename = $row['path']; |
---|
87 | if (is_file($filename)) |
---|
88 | { |
---|
89 | $videos++; |
---|
90 | //echo $filename; |
---|
91 | $fileinfo = $getID3->analyze($filename); |
---|
92 | //print_r($fileinfo); |
---|
93 | $exif = array(); |
---|
94 | if (isset($fileinfo['filesize'])) |
---|
95 | { |
---|
96 | $exif['filesize'] = $fileinfo['filesize']; |
---|
97 | } |
---|
98 | /* |
---|
99 | if (isset($fileinfo['playtime_string'])) |
---|
100 | { |
---|
101 | $exif['playtime_string'] = $fileinfo['playtime_string']; |
---|
102 | } |
---|
103 | */ |
---|
104 | if (isset($fileinfo['video']['resolution_x'])) |
---|
105 | { |
---|
106 | $exif['width'] = $fileinfo['video']['resolution_x']; |
---|
107 | } |
---|
108 | if (isset($fileinfo['video']['resolution_y'])) |
---|
109 | { |
---|
110 | $exif['height'] = $fileinfo['video']['resolution_y']; |
---|
111 | } |
---|
112 | if (isset($fileinfo['tags']['quicktime']['gps_latitude'][0]) and $sync_options['sync_gps']) |
---|
113 | { |
---|
114 | $exif['lat'] = $fileinfo['tags']['quicktime']['gps_latitude'][0]; |
---|
115 | } |
---|
116 | if (isset($fileinfo['tags']['quicktime']['gps_longitude'][0]) and $sync_options['sync_gps']) |
---|
117 | { |
---|
118 | $exif['lon'] = $fileinfo['tags']['quicktime']['gps_longitude'][0]; |
---|
119 | } |
---|
120 | if (isset($fileinfo['tags']['quicktime']['model'][0])) |
---|
121 | { |
---|
122 | $exif['Model'] = substr($fileinfo['tags']['quicktime']['model'][0], 2); |
---|
123 | } |
---|
124 | if (isset($fileinfo['tags']['quicktime']['software'][0])) |
---|
125 | { |
---|
126 | $exif['Model'] .= " ". substr($fileinfo['tags']['quicktime']['software'][0], 2); |
---|
127 | } |
---|
128 | if (isset($fileinfo['tags']['quicktime']['make'][0])) |
---|
129 | { |
---|
130 | $exif['Make'] = $fileinfo['tags']['quicktime']['make'][0]; |
---|
131 | } |
---|
132 | if (isset($fileinfo['tags']['quicktime']['creation_date'][0])) |
---|
133 | { |
---|
134 | // ?2013-03-25T09:46:54+0900 |
---|
135 | $exif['DateTimeOriginal'] = substr($fileinfo['tags']['quicktime']['creation_date'][0], 1); |
---|
136 | } |
---|
137 | //print_r($exif); |
---|
138 | if (isset($exif) and $sync_options['metadata']) |
---|
139 | { |
---|
140 | $metadata++; |
---|
141 | $infos[] = $filename. ' metadata: '.implode(",", array_keys($exif)); |
---|
142 | if ($sync_options['metadata'] and !$sync_options['simulate']) |
---|
143 | { |
---|
144 | $dbfields = explode(",", "filesize,width,height,lat,lon"); |
---|
145 | $query = "UPDATE ".IMAGES_TABLE." SET ".vjs_dbSet($dbfields, $exif).", `date_metadata_update`=CURDATE() WHERE `id`=".$row['id'].";"; |
---|
146 | pwg_query($query); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | if ($sync_options['thumb']) |
---|
151 | { |
---|
152 | $thumbs++; |
---|
153 | /* Init value */ |
---|
154 | $file_wo_ext = pathinfo($row['file']); |
---|
155 | $output_dir = dirname($row['path']) . '/pwg_representative/'; |
---|
156 | $in = $filename; |
---|
157 | $out = $output_dir.$file_wo_ext['filename'].'.'.$sync_options['thumbouput']; |
---|
158 | /* report it */ |
---|
159 | $infos[] = $filename. ' thumbnail: '.$out; |
---|
160 | |
---|
161 | if (!is_dir($output_dir) or !is_writable($output_dir)) |
---|
162 | { |
---|
163 | $errors[] = "Directory ".$output_dir." doesn't exist or wrong permission"; |
---|
164 | } |
---|
165 | else if ($sync_options['thumbsec'] and !$sync_options['simulate']) |
---|
166 | { |
---|
167 | if (isset($fileinfo['playtime_seconds']) and $sync_options['thumbsec'] > $fileinfo['playtime_seconds']) |
---|
168 | { |
---|
169 | $errors[] = "Movie ". $filename." is shorter than ". $sync_options['thumbsec'] ." secondes, fallback to 1 secondes"; |
---|
170 | $sync_options['thumbsec'] = 1; |
---|
171 | } |
---|
172 | $ffmpeg = "ffmpeg -itsoffset -".$sync_options['thumbsec']." -i ".$in." -vcodec mjpeg -vframes 1 -an -f rawvideo -y ".$out; |
---|
173 | if ($sync_options['thumbouput'] == "png") |
---|
174 | { |
---|
175 | $ffmpeg = "ffmpeg -itsoffset -".$sync_options['thumbsec']." -i ".$in." -vcodec png -vframes 1 -an -f rawvideo -y ".$out; |
---|
176 | } |
---|
177 | //echo $ffmpeg; |
---|
178 | $log = system($ffmpeg, $retval); |
---|
179 | //$infos[] = $filename. ' thumbnail : retval:'. $retval. ", log:". print_r($log, True); |
---|
180 | if($retval != 0 or !file_exists($out)) |
---|
181 | { |
---|
182 | $errors[] = "Error running ffmpeg, try it manually:\n<br/>". $ffmpeg; |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | /* Update DB */ |
---|
187 | $query = "UPDATE ".IMAGES_TABLE." SET `representative_ext`='".$sync_options['thumbouput']."' WHERE `id`=".$row['id'].";"; |
---|
188 | pwg_query($query); |
---|
189 | |
---|
190 | /* Delete any previous square or thumbnail or small images */ |
---|
191 | $idata = "_data/i/".dirname($row['path']).'/pwg_representative/'; |
---|
192 | $extensions = array('-th.jpg', '-sq.jpg', '-th.png', '-sq.png', '-sm.png', '-sm.png'); |
---|
193 | foreach ($extensions as $extension) |
---|
194 | { |
---|
195 | $ifile = $idata.$file_wo_ext['filename'].$extension; |
---|
196 | if(is_file($ifile)) |
---|
197 | { |
---|
198 | unlink($ifile); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /* Generate the overlay */ |
---|
203 | if($sync_options['thumboverlay']) |
---|
204 | { |
---|
205 | add_movie_frame($out); |
---|
206 | $infos[] = $filename. ' overlay: '.$out; |
---|
207 | } |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | /*************** |
---|
215 | * |
---|
216 | * End the sync work |
---|
217 | * |
---|
218 | */ |
---|