/** * Get the duration of an audio or video file. * * @param $node * The node to which the audio or video file is attached. * * @param $filepath * The path to the audio or video file. * * @return * A string in the form "HH:MM:SS" representing the duration in hours, * minutes and seconds. */ function flashvideo_get_duration($node, $filepath) { if ($filepath && variable_get('flashvideo_' . $node->type .'_ffmpegphp', 0)) { $extension = "ffmpeg"; $extension_soname = $extension . "." . PHP_SHLIB_SUFFIX; $extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;
// Load extension. if (!extension_loaded($extension)) { dl($extension_soname); }
// Use FFMPEG-PHP to get video information. $movie = new ffmpeg_movie($filepath); }
// Get the duration of the movie or audio file in seconds. if($filepath && $movie) { $duration = $movie->getDuration(); } else { $duration = 0; }
Just started using a variation of this code... thanks colan!
To answer the above, this should work to get the filepath...
Keep in mind that if you are deleting the original file after conversion, this will fail when you update the node after it has been converted. You'll need to wrap where you set $node_field[0]['value'] in some conditional statements to prevent it from being recalculated if the the query fails or file doesn't exist...
if (!$node->nid) node_save($node); $filepath = '/var/www/drupal/'; // set this to the full path to your drupal root $filepath .= db_result(db_query("SELECT filepath FROM {files} WHERE (nid = '" . $node->nid . "') AND ((filepath NOT like '%flv') AND (filepath NOT like '%jpg'))")); $node_field[0]['value'] = flashvideo_get_duration($node, $filepath);
Thanks a lot for sharing that piece of code. I am trying to use it in my site, but there seems to be some db query problem:
user warning: Unknown column 'nid' in 'where clause' query: SELECT filepath FROM files WHERE (nid = '82') AND ((filepath NOT like '0lv') AND (filepath NOT like '%jpg')) in /var/www/sites/default/modules/computed_field/computed_field.module(138) : eval()'d code on line 4.
Can you please share some more information if nid shouldn't be called from "node" instead of "files" table?
Sorry, a bit late... just noticed your reply. I'm sure you have figured this out by now.
You need to query the files table to get the filepath for a file attached to that node, that is not in the node table. I would double check your syntax?
Here is the full code we're using in a computed field.
This is for Drupal 5.
if (!$node->nid) $node_save($node);
$filepath_base = "/var/www/drupal/"; if ($filepath_db = db_result(db_query("SELECT filepath FROM {files} WHERE (nid = %d) AND ((filepath like '%mp4') OR (filepath like '%mov') OR (filepath like '%mp3'))", $node->nid))) $filepath = $filepath_base . $filepath_db;
// Load extension. if (!extension_loaded($extension)) { dl($extension_soname); }
// Use FFMPEG-PHP to get video information. $movie = new ffmpeg_movie($filepath); }
// Get the duration of the movie or audio file in seconds. if($movie) { $duration = $movie->getDuration(); } else { $duration_error_msg = 'Could not get duration for ' . $filepath; drupal_set_message($duration_error_msg, 'error'); $duration = 0; }
Yes, actually I have figured that out and it was working fine. Now I am trying to use filefield to upload files instead of the Drupal's build-in upload module. The problem now is that I am not able to figure out a relation between fid and nid when I am trying to get the filepath from the {files} table.
I am using "computed field" as a CCK when I create my node, but for some reason my code gets runned just before the node is saved and database populated and thus can not fetch the filepath, which usualy should be into the {files} table.
Comments
Use FFMPEG-PHP
You could add a function like this:
/**
* Get the duration of an audio or video file.
*
* @param $node
* The node to which the audio or video file is attached.
*
* @param $filepath
* The path to the audio or video file.
*
* @return
* A string in the form "HH:MM:SS" representing the duration in hours,
* minutes and seconds.
*/
function flashvideo_get_duration($node, $filepath) {
if ($filepath &&
variable_get('flashvideo_' . $node->type .'_ffmpegphp', 0)) {
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;
// Load extension.
if (!extension_loaded($extension)) {
dl($extension_soname);
}
// Use FFMPEG-PHP to get video information.
$movie = new ffmpeg_movie($filepath);
}
// Get the duration of the movie or audio file in seconds.
if($filepath && $movie) {
$duration = $movie->getDuration();
}
else {
$duration = 0;
}
// Return the duration as a formatted "HH:MM:SS" string.
$hours = $duration / 3600;
$minutes = ($duration % 3600) / 60;
$seconds = ($duration % 3600) % 60;
return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
where would i add that i am
where would i add that
i am trying to get the duration to show up as a vews filed
Free Chemistry Tutoring
Computed Field
I haven't really thought about this much, so there may be a better way, but the following comes to mind:
You can have a field of type Computed Field which calls the above function. That would probably give you what you want.
Newbie: filepath?
Newbie Question: I am unable to get the file path from flashvideo to pass to the function.
Can anyone please give me some pointers regarding that.
Thanks
yeah i could never get this
yeah i could never get this to work and i really need it to
Free Chemistry Tutoring
Just started using a
Just started using a variation of this code... thanks colan!
To answer the above, this should work to get the filepath...
Keep in mind that if you are deleting the original file after conversion, this will fail when you update the node after it has been converted. You'll need to wrap where you set $node_field[0]['value'] in some conditional statements to prevent it from being recalculated if the the query fails or file doesn't exist...
if (!$node->nid) node_save($node);$filepath = '/var/www/drupal/'; // set this to the full path to your drupal root
$filepath .= db_result(db_query("SELECT filepath FROM {files} WHERE (nid = '" . $node->nid . "') AND ((filepath NOT like '%flv') AND (filepath NOT like '%jpg'))"));
$node_field[0]['value'] = flashvideo_get_duration($node, $filepath);
Hi jtolj, Thanks a lot for
Hi jtolj,
Thanks a lot for sharing that piece of code. I am trying to use it in my site, but there seems to be some db query problem:
user warning: Unknown column 'nid' in 'where clause' query: SELECT filepath FROM files WHERE (nid = '82') AND ((filepath NOT like '0lv') AND (filepath NOT like '%jpg')) in /var/www/sites/default/modules/computed_field/computed_field.module(138) : eval()'d code on line 4.
Can you please share some more information if nid shouldn't be called from "node" instead of "files" table?
Cheers,
a bit late but...
Hi Sydbata,
Sorry, a bit late... just noticed your reply. I'm sure you have figured this out by now.
You need to query the files table to get the filepath for a file attached to that node, that is not in the node table. I would double check your syntax?
Here is the full code we're using in a computed field.
This is for Drupal 5.
if (!$node->nid) $node_save($node);
$filepath_base = "/var/www/drupal/";
if ($filepath_db = db_result(db_query("SELECT filepath FROM {files} WHERE (nid = %d) AND ((filepath like '%mp4') OR (filepath like '%mov') OR (filepath like '%mp3'))", $node->nid))) $filepath = $filepath_base . $filepath_db;
if ($filepath && file_exists($filepath)) {
if (variable_get('flashvideo_' . $node->type .'_ffmpegphp', 0)) {
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;
// Load extension.
if (!extension_loaded($extension)) {
dl($extension_soname);
}
// Use FFMPEG-PHP to get video information.
$movie = new ffmpeg_movie($filepath);
}
// Get the duration of the movie or audio file in seconds.
if($movie) {
$duration = $movie->getDuration();
}
else {
$duration_error_msg = 'Could not get duration for ' . $filepath;
drupal_set_message($duration_error_msg, 'error');
$duration = 0;
}
// Return the duration as a formatted "HH:MM:SS" string.
$hours = $duration / 3600;
$minutes = ($duration % 3600) / 60;
$seconds = ($duration % 3600) % 60;
$node_field[0]['value'] = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
it worked before
hi jtolj,
Yes, actually I have figured that out and it was working fine. Now I am trying to use filefield to upload files instead of the Drupal's build-in upload module. The problem now is that I am not able to figure out a relation between fid and nid when I am trying to get the filepath from the {files} table.
I am using "computed field" as a CCK when I create my node, but for some reason my code gets runned just before the node is saved and database populated and thus can not fetch the filepath, which usualy should be into the {files} table.
How about drupal 6 ?
Hi,
Did anyone tried the code here for Drupal 6?
Thanks
NaZ
It's working on D6 after minor modifications.
It's working on D6 after minor modifications. File path is no longer a parameter.
function flashvideo_get_duration($node) {
$videofile = flashvideo_get_video_file($node->nid, 0);
$filepath = $videofile->filepath;
if ($filepath && variable_get('flashvideo_ffmpegphp', 0)) {
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;
// Load extension.
if (!extension_loaded($extension)) {
dl($extension_soname);
}
// Use FFMPEG-PHP to get video information.
$movie = new ffmpeg_movie($filepath);
}
// Get the duration of the movie or audio file in seconds.
if($filepath && $movie) {
$duration = $movie->getDuration();
}
else {
$duration = 0;
}
// Return the duration as a formatted "HH:MM:SS" string.
$hours = $duration / 3600;
$minutes = ($duration % 3600) / 60;
$seconds = ($duration % 3600) % 60;
return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
You can use this in your template file:
print flashvideo_get_duration($node);