DaDaBIK
This forum is devoted to the discussion about the software DaDaBIK database front-end
the PHP Database application generator for MySQL, PostgreSQL and SQLite; also available for Wordpress.

Welcome! Log In Create A New Profile

Advanced

uploading video

Posted by darren 
uploading video
March 21, 2019 01:39PM
Hello,

I have imported fancybox into my dadabik application so that when i open pictures it enlarges and is able to be manipulated etc. Now I would like to be able to import videos. however, this will be much more complicated than pictures as they will typically need to be converted to .mp4 so that they can be played natively on html5. Because of this I would need to alter the upload process that dadabik uses.

I am unable to do that since it seems that the code for uploading a file is within an encrypted file. I could be wrong but even if it wasnt encrypted I would still like any suggestions on whether or not I would need to actually change the code for uploading.

Any suggestions on how I could implement this into dadabik?

I know a simple solution would be using ffmpeg and a code like

ffmpeg -i {input}.mov -vcodec h264 -acodec aac -strict -2 {output}.mp4

PHP Version: 7.2.14-1+ubuntu16.04.1+deb.sury.org+1

postgres version: 10.6 (Ubuntu 10.6-1.pgdg16.04+1)

Web server: Apache/2.4.18 (Ubuntu)

Client: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36



Edited 1 time(s). Last edit at 03/21/2019 01:40PM by darren.
Re: uploading video
March 22, 2019 01:12AM
Hello,
I can't imagine any simple solution, maybe you can schedule a cronjob (e.g. every minute) on your server that converts the files? It wouldn't be in real time but it's something.

I will think about adding an after_upload hook, it might be useful.

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
March 22, 2019 08:47AM
do you think that a before insert hook might get this to work?
Re: uploading video
March 22, 2019 11:29AM
No, because the before insert hook is triggered before the execution of the upload.

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
March 22, 2019 12:52PM
is the after insert hook also before the upload?
Re: uploading video
March 23, 2019 11:01AM
No, it is after.

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
March 26, 2019 10:01AM
So i made an after insert hook which seemed like the solution but the hook doesnt seem to be passing the auto increment id of the item just inserted.

here is the code I am using, its far from ready but I wanted a working prototype:

$hooks['cat_files']['insert']['after'] = 'dadabik_convert_video';
function dadabik_convert_video ($vid_id){
	global $conn;
	
	//select name of video file
	$file_sql = "SELECT file FROM cat_files WHERE id = $vid_id ";
	//just to see if the $vid_id variable actually contains anything
	var_dump($vid_id);
//working code for sql statement and video conversion, will need to be cleaned up
$file_result = $conn->prepare($file_sql);

$file_result->execute();

$array = $file_result->fetch(PDO::FETCH_ASSOC);

$results = array();

$results[] = $array;

while($row = $file_result->fetch(PDO::FETCH_ASSOC)){
	
$results[] = [
	'file' => $row['file']
	];

}

$file_name = $results['0']['file'];
//check if file is video file
$content = mime_content_type("./uploads/" . strtolower($file_name));


//video conversion
if (strpos($content, 'video') !== false) {
	$file_no_extension_array = explode(".",$file_name); 
	$file_no_extension = $file_no_extension_array['0'];
	exec("ffmpeg -i ./uploads/" . $file_name . " -vcodec h264 -acodec aac -strict -2 ./uploads/" . $file_no_extension . ".mp4 > /dev/null &");

//update old record with new converted video file name
$sql = "UPDATE cat_files
		SET file = " . $file_no_extension . ".mp4
		WHERE id = ':id'";
$prepare = $conn->prepare($sql);
$execute = $prepare->execute();

} else { 
//if the uploaded file is not a video, then do nothing
return null; 
 }
	
}

but the error i am receiving is that the variable $vid_id does not contain any value as in when i do a var_dump of the variable it evaluates to "false". when i looked at your tutorial it seemed to say that the id of the column just inserted will be in the function variable i.e.

function dadabik_convert_video ($vid_id)

Am i missing something?
Re: uploading video
March 26, 2019 10:21AM
I haven't checked all your code but about the input variable, apparently, you haven't missed anything.

Are you sure the field it's an auto increment field? Which DBMS are you using, MySQL?

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
March 27, 2019 08:27AM
yeah its an auto increment field, and im using PostegreSQL
Re: uploading video
March 29, 2019 04:42AM
Hello,
ok so the reason is PostgreSQL, from the documentation:

"Please note that on PostgreSQL the after insert hook doesn't fully work (it doesn't receive the value of the primary key of the record just inserted)."

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
April 02, 2019 03:27AM
Hi

Can this be circumvented by having a trigger that adds the primary key to a second field before_insert

Begin
set @auto_id := (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='xxxxxxxxx' AND TABLE_SCHEMA=DATABASE() );
set NEW.id2 = @auto_id;
end

which then can be queried from the after_insert hook in DaDaBIK

Best

Stefan
Re: uploading video
April 03, 2019 03:54AM
Hi Stefan,
I am not sure about this: you can save the primary key in a second field but how can the after_insert function know the value?

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Re: uploading video
April 10, 2019 08:41AM
I was reading into why this may be caused in postgresql, and I can only guess since I cant read the code, however, it seems postgres needs to explicitly have a RETURN element to any insert or update (or delete i would assume)

the code would need to be like

INSERT INTO persons (lastname,firstname) VALUES ('Smith', 'John') RETURNING id;

of course the returning field would not be dynamic and need to be explicitly typed each time (maybe the primary key field used in the table configuration). However, functions like LASTVAL() are dynamic and would return the new number for the last modified sequence. I could not use LASTVAL() because most of my tables are views that have instead of triggers so the LASTVAL() for me would be useless since it would return the sequence number from a child table and not the parent table.

Anyway, again I am only guessing what might be the cause of this issue with POSTGRESQL. I pulled most of this information from this page
Re: uploading video
April 12, 2019 01:20AM
Hello Darren,
yes I am aware of the way postgresql handles this but the implementation in DaDaBIK wasn't easy and at the moment it's still in stand-by.

Best,

________________________________________________________
Eugenio |\.-./| DaDaBIK developer and project admin
Sorry, only registered users may post in this forum.

Click here to login