2020-05-16 18:18:32 +00:00
< ? php
2020-05-23 21:47:15 +00:00
require 'Database.php' ;
2020-06-03 16:26:30 +00:00
require 'TMDBMovie.php' ;
2020-06-04 20:19:18 +00:00
writeLog ( " starting extraction! \n " );
2020-06-02 14:15:12 +00:00
$ffmpeg = 'ffmpeg' ; //or: /usr/bin/ffmpeg , or /usr/local/bin/ffmpeg - depends on your installation (type which ffmpeg into a console to find the install path)
2020-06-03 16:26:30 +00:00
$tmdb = new TMDBMovie ();
2020-06-12 18:09:54 +00:00
// initial load of all available movie genres
2020-06-08 15:52:58 +00:00
$tmdbgenres = $tmdb -> getAllGenres ();
2020-05-23 21:47:15 +00:00
$conn = Database :: getInstance () -> getConnection ();
2020-06-03 16:26:30 +00:00
$scandir = " ../videos/prn/ " ;
$arr = scandir ( $scandir );
2020-05-16 18:18:32 +00:00
2020-05-23 21:47:15 +00:00
$all = 0 ;
$added = 0 ;
2020-05-27 19:35:02 +00:00
$deleted = 0 ;
2020-05-23 21:47:15 +00:00
$failed = 0 ;
2020-05-16 18:18:32 +00:00
foreach ( $arr as $elem ) {
2020-05-23 21:47:15 +00:00
if ( $elem != " . " && $elem != " .. " ) {
2020-06-03 16:26:30 +00:00
if ( strpos ( $elem , '.mp4' ) !== false ) {
$moviename = substr ( $elem , 0 , - 4 );
$query = " SELECT * FROM videos WHERE movie_name = ' " . mysqli_real_escape_string ( $conn , $moviename ) . " ' " ;
$result = $conn -> query ( $query );
// insert if not available in db
if ( ! mysqli_fetch_assoc ( $result )) {
// try to fetch data from TMDB
2020-06-03 22:15:06 +00:00
$poster = - 1 ;
2020-06-04 14:45:24 +00:00
$genres = - 1 ;
2020-06-03 16:26:30 +00:00
if ( ! is_null ( $dta = $tmdb -> searchMovie ( $moviename ))) {
$pic = file_get_contents ( $tmdb -> picturebase . $dta -> poster_path );
2020-06-03 22:15:06 +00:00
$poster = shell_exec ( " ffmpeg -hide_banner -loglevel panic -ss 00:04:00 -i \" ../videos/prn/ $elem\ " - vframes 1 - q : v 2 - f singlejpeg pipe : 1 2 >/ dev / null " );
2020-06-04 14:45:24 +00:00
2020-06-12 18:09:54 +00:00
// error handling for download error
if ( ! $pic ) {
$pic = $poster ;
$poster = - 1 ;
echo " Failed to load Picture from TMDB! \n " ;
2020-06-08 17:54:20 +00:00
}
2020-06-04 14:45:24 +00:00
$genres = $dta -> genre_ids ;
2020-06-03 16:26:30 +00:00
} else {
echo " nothing found with TMDB! \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " nothing found with TMDB! \n " );
2020-06-03 16:26:30 +00:00
$pic = shell_exec ( " ffmpeg -hide_banner -loglevel panic -ss 00:04:00 -i \" ../videos/prn/ $elem\ " - vframes 1 - q : v 2 - f singlejpeg pipe : 1 2 >/ dev / null " );
}
2020-05-23 21:47:15 +00:00
2020-06-03 22:15:06 +00:00
//convert video to base64
2020-06-03 16:26:30 +00:00
$image_base64 = base64_encode ( $pic );
2020-06-03 22:15:06 +00:00
// add base64 fileinfo
2020-06-03 16:26:30 +00:00
$image = 'data:image/jpeg;base64,' . $image_base64 ;
2020-05-23 21:47:15 +00:00
2020-06-03 22:15:06 +00:00
// extract other video attributes
2020-06-03 16:26:30 +00:00
$video_attributes = _get_video_attributes ( $elem );
2020-06-03 22:15:06 +00:00
$duration = 0 ;
$size = 0 ;
$width = 0 ;
if ( $video_attributes ) {
$duration = $video_attributes -> media -> track [ 0 ] -> Duration ; // in seconds
$size = $video_attributes -> media -> track [ 0 ] -> FileSize ; // in Bytes
$width = $video_attributes -> media -> track [ 1 ] -> Width ; // width
}
2020-06-02 14:15:12 +00:00
2020-06-03 22:15:06 +00:00
if ( $poster != - 1 ) {
$poster_base64 = 'data:image/jpeg;base64,' . base64_encode ( $poster );
2020-06-02 14:15:12 +00:00
2020-06-03 22:15:06 +00:00
$query = " INSERT INTO videos(movie_name,movie_url,poster,thumbnail,quality,length)
2020-06-03 16:26:30 +00:00
VALUES ( '" . mysqli_real_escape_string($conn, $moviename) . "' ,
2020-06-02 14:15:12 +00:00
'" . mysqli_real_escape_string($conn, ' videos / prn / ' . $elem) . "' ,
2020-06-03 22:15:06 +00:00
'$poster_base64' ,
2020-06-02 14:15:12 +00:00
'$image' ,
2020-06-03 22:15:06 +00:00
'$width' ,
2020-06-02 14:15:12 +00:00
'$duration' ) " ;
2020-06-03 22:15:06 +00:00
} else {
$query = " INSERT INTO videos(movie_name,movie_url,thumbnail,quality,length)
VALUES ( '" . mysqli_real_escape_string($conn, $moviename) . "' ,
'" . mysqli_real_escape_string($conn, ' videos / prn / ' . $elem) . "' ,
'$image' ,
'$width' ,
'$duration' ) " ;
}
2020-05-23 21:47:15 +00:00
2020-06-03 16:26:30 +00:00
if ( $conn -> query ( $query ) === TRUE ) {
echo ( 'successfully added ' . $elem . " to video gravity \n " );
2020-06-04 20:19:18 +00:00
writeLog ( 'successfully added ' . $elem . " to video gravity \n " );
2020-06-08 15:52:58 +00:00
// add this entry to the default tags
2020-06-03 16:26:30 +00:00
$last_id = $conn -> insert_id ;
// full hd
2020-06-03 22:15:06 +00:00
if ( $width >= 1900 ) {
2020-06-03 16:26:30 +00:00
$query = " INSERT INTO video_tags(video_id,tag_id) VALUES ( $last_id ,2) " ;
if ( $conn -> query ( $query ) !== TRUE ) {
echo " failed to add default tag here. \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " failed to add default tag here. \n " );
2020-06-03 16:26:30 +00:00
}
2020-06-02 14:15:12 +00:00
}
2020-06-08 15:52:58 +00:00
// HD
2020-06-03 22:15:06 +00:00
if ( $width >= 1250 && $width < 1900 ) {
2020-06-03 16:26:30 +00:00
$query = " INSERT INTO video_tags(video_id,tag_id) VALUES ( $last_id ,4) " ;
if ( $conn -> query ( $query ) !== TRUE ) {
echo " failed to add default tag here. \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " failed to add default tag here. \n " );
2020-06-03 16:26:30 +00:00
}
2020-06-02 14:15:12 +00:00
}
2020-06-08 15:52:58 +00:00
// SD
2020-06-04 20:19:18 +00:00
if ( $width < 1250 && $width > 0 ) {
2020-06-03 16:26:30 +00:00
$query = " INSERT INTO video_tags(video_id,tag_id) VALUES ( $last_id ,3) " ;
if ( $conn -> query ( $query ) !== TRUE ) {
echo " failed to add default tag here. \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " failed to add default tag here. \n " );
2020-06-03 16:26:30 +00:00
}
2020-06-02 14:15:12 +00:00
}
2020-06-04 14:45:24 +00:00
// handle tmdb genres here!
2020-06-04 20:19:18 +00:00
if ( $genres != - 1 ) {
2020-06-08 15:52:58 +00:00
// transform genre ids in valid names
foreach ( $genres as $genreid ) {
// check if genre is already a tag in db if not insert it
$tagname = array_column ( $tmdbgenres , 'name' , 'id' )[ $genreid ];
$tagid = tagExists ( $tagname );
$query = " INSERT INTO video_tags(video_id,tag_id) VALUES ( $last_id , $tagid ) " ;
if ( $conn -> query ( $query ) !== TRUE ) {
echo " failed to add $genreid tag here. \n " ;
writeLog ( " failed to add $genreid tag here. \n " );
}
2020-06-04 14:45:24 +00:00
}
}
2020-06-03 16:26:30 +00:00
$added ++ ;
$all ++ ;
} else {
echo ( 'errored item: ' . $elem . " \n " );
2020-06-04 20:19:18 +00:00
writeLog ( 'errored item: ' . $elem . " \n " );
2020-06-03 16:26:30 +00:00
echo ( '{"data":"' . $conn -> error . '"}\n' );
2020-06-04 20:19:18 +00:00
writeLog ( '{"data":"' . $conn -> error . '"}\n' );
2020-06-03 16:26:30 +00:00
$failed ++ ;
2020-06-02 14:15:12 +00:00
}
2020-05-23 21:47:15 +00:00
} else {
2020-06-03 16:26:30 +00:00
$all ++ ;
2020-05-23 21:47:15 +00:00
}
2020-06-03 22:15:06 +00:00
} else {
2020-06-03 16:26:30 +00:00
echo ( $elem . " does not contain a .mp4 extension! - skipping \n " );
2020-06-04 20:19:18 +00:00
writeLog ( $elem . " does not contain a .mp4 extension! - skipping \n " );
2020-05-23 21:47:15 +00:00
}
}
2020-05-16 18:18:32 +00:00
}
2020-05-23 21:47:15 +00:00
2020-06-08 15:52:58 +00:00
// auto cleanup db entries
2020-05-27 19:35:02 +00:00
$query = " SELECT COUNT(*) as count FROM videos " ;
$result = $conn -> query ( $query );
$r = mysqli_fetch_assoc ( $result );
if ( $all < $r [ 'count' ]) {
echo " should be in gravity: " . $all . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " should be in gravity: " . $all . " \n " );
2020-05-27 19:35:02 +00:00
echo " really in gravity: " . $r [ 'count' ] . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " really in gravity: " . $r [ 'count' ] . " \n " );
2020-05-27 19:35:02 +00:00
echo " cleaning up gravity \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " cleaning up gravity \n " );
2020-05-27 19:35:02 +00:00
$query = " SELECT movie_id,movie_url FROM videos " ;
$result = $conn -> query ( $query );
while ( $r = mysqli_fetch_assoc ( $result )) {
if ( ! file_exists ( " ../ " . $r [ 'movie_url' ])) {
$query = " DELETE FROM videos WHERE movie_id=' " . $r [ 'movie_id' ] . " ' " ;
if ( $conn -> query ( $query ) === TRUE ) {
echo ( 'successfully deleted ' . $r [ 'movie_url' ] . " from video gravity \n " );
2020-06-04 20:19:18 +00:00
writeLog ( 'successfully deleted ' . $r [ 'movie_url' ] . " from video gravity \n " );
2020-05-27 19:35:02 +00:00
$deleted ++ ;
} else {
echo " failed to delete " . $r [ 'movie_url' ] . " from gravity: " . $conn -> error . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " failed to delete " . $r [ 'movie_url' ] . " from gravity: " . $conn -> error . " \n " );
2020-05-27 19:35:02 +00:00
}
}
}
}
// calculate size of databse here
$size = - 1 ;
$query = " SELECT table_schema AS \" Database \" ,
ROUND ( SUM ( data_length + index_length ) / 1024 / 1024 , 3 ) AS \ " Size \"
FROM information_schema . TABLES
2020-06-03 22:15:06 +00:00
WHERE TABLE_SCHEMA = '" . Database::getInstance()->getDatabaseName() . "'
2020-05-27 19:35:02 +00:00
GROUP BY table_schema ; " ;
$result = $conn -> query ( $query );
if ( $result -> num_rows == 1 ) {
$row = $result -> fetch_assoc ();
$size = $row [ " Size " ];
}
2020-05-23 21:47:15 +00:00
echo " Total gravity: " . $all . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " Total gravity: " . $all . " \n " );
2020-05-27 19:35:02 +00:00
echo " Size of Databse is: " . $size . " MB \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " Size of Databse is: " . $size . " MB \n " );
2020-05-23 21:47:15 +00:00
echo " added in this run: " . $added . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " added in this run: " . $added . " \n " );
2020-05-27 19:35:02 +00:00
echo " deleted in this run: " . $deleted . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " deleted in this run: " . $deleted . " \n " );
2020-06-02 14:15:12 +00:00
echo " errored in this run: " . $failed . " \n " ;
2020-06-04 20:19:18 +00:00
writeLog ( " errored in this run: " . $failed . " \n " );
2020-06-02 14:15:12 +00:00
2020-06-08 15:52:58 +00:00
writeLog ( " -42 " ); // terminating characters to stop webui requesting infos
/**
* get all videoinfos of a video file
*
* @ param $video string name including extension
* @ return object all infos as object
*/
2020-06-02 14:15:12 +00:00
function _get_video_attributes ( $video )
{
2020-06-03 22:15:06 +00:00
$command = " mediainfo \" ../videos/prn/ $video\ " -- Output = JSON " ;
2020-06-02 14:15:12 +00:00
$output = shell_exec ( $command );
2020-06-03 22:15:06 +00:00
return json_decode ( $output );
2020-06-02 14:15:12 +00:00
}
2020-06-04 20:19:18 +00:00
2020-06-08 15:52:58 +00:00
/**
* write a line to the output log file
*
* @ param string $message message to write
*/
2020-06-04 20:19:18 +00:00
function writeLog ( string $message )
{
file_put_contents ( " /tmp/output.log " , $message , FILE_APPEND );
flush ();
}
2020-06-08 15:52:58 +00:00
/**
* ckecks if tag exists -- if not creates it
* @ param string $tagname the name of the tag
* @ return integer the id of the inserted tag
*/
function tagExists ( string $tagname )
{
global $conn ;
$query = " SELECT * FROM tags WHERE tag_name=' $tagname ' " ;
$result = $conn -> query ( $query );
if ( $result -> num_rows == 0 ) {
// tag does not exist --> create it
$query = " INSERT INTO tags (tag_name) VALUES (' $tagname ') " ;
if ( $conn -> query ( $query ) !== TRUE ) {
echo " failed to create $tagname tag in database \n " ;
writeLog ( " failed to create $tagname tag in database \n " );
}
return $conn -> insert_id ;
} else {
return $result -> fetch_assoc ()[ 'tag_id' ];
}
}