uploading video

darren

Member
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

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

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
 

eugenio

Administrator
Staff member
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

Administrator
Staff member
No, because the before insert hook is triggered before the execution of the upload.

Best,
 

darren

Member
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:

[pre]
$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;
}

}
[/pre]

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.

[pre]
function dadabik_convert_video ($vid_id)
[/pre]

Am i missing something?
 

eugenio

Administrator
Staff member
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

Administrator
Staff member
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,
 

taubes

Member
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
 

eugenio

Administrator
Staff member
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,
 

darren

Member
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

[pre]
INSERT INTO persons (lastname,firstname) VALUES ('Smith', 'John') RETURNING id;
[/pre]

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
 

eugenio

Administrator
Staff member
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,
 
Top