ffmpeg not detecting video file duration - php

Am using the following code to detect video duration before uploading using ffmpeg, but it doesn't seem to work:
<?php
$file=sample.avi;
$time = exec("$ffmpeg -i /path/".$file." 2>&1 |
grep Duration | cut -d ' ' -f 4 | sed s/,//");
echo $time;
?>
I need to echo video duration as text input to sql before uploading file to server.

i used https://github.com/wseemann/FFmpegMediaMetadataRetriever
with following code to retrive video duration
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
String time = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
int VideoDuration = Integer.parseInt(time);
mmr.release();

Related

Adding numbers and getting a percentage

I have a PHP script that runs a few du commands to extract the kB size of 3 specific directories. That works, and I have $dir1size as 521046477, dir2size as 521043911 and $dir3size as 67167152. These pop out fine (ie no dodgy characters in the strings) if I echo the results into a JSON format.
I then want to add them up to get $tot and divide by the disk size which is $disksize to get $pctused.
The code I have is...
$rtn = array();
# Get disk size
$cmd = "df | sed -n '/DataVolume/s/ \+/ /gp' | cut -d' ' -f 2";
exec($cmd, $disksize, $int);
$rtn["disk"]["kB"]["total"] = $disksize[0];
# Get dir1 size
$cmd = "du --apparent-size .. | sed -n 's/[\ ]*\.\.\/TNFrontCam1$/ TNFront/p' | cut -d' ' -f 1";
exec($cmd, $dir1size, $int);
$rtn["disk"]["kB"]["TNFront"] = $dir1size[0];
# 2 more blocks like that for the other 2 directories
# Do the calulations
$tot = $dir1size + $dir2size + $dir3size;
$rtn["disk"]["kB"]["used"] = $tot;
$pctused = ($tot / $disksize) * 100;
$rtn["disk"]["percent"]["used"] = $pctused;
$rtn["disk"]["percent"]["free"] = 100 - $pctused;
echo json_encode($rtn, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
When I run this get Fatal Error: Unsupported operand types in the line calculating the percentage used.
If I comment out the 3 percentage lines I get the JSON string but instead of showing me the sum of the 3, I just get $dir1size but in square brackets! (hence the fatal error when it tries to do the percent calcs).
I've also tried intval($dirxsize) but then I get a null in the JSON output.
(I should add I am running this on a WD NAS drive that I've SSHed into in the hope that I can report usage info to my Home Assistant)
Edit - When I use PHP Sandbox, the maths all works out fine!
The second parameter of the exec function is an array (one element for each line of output), so if the size is on the first line you have to access the element at index zero, like:
$disksize[0]
For every variable used as the second argument of exec:
$tot = $dir1size[0] + $dir2size[0] + $dir3size[0];
$rtn["disk"]["kB"]["used"] = $tot;
$pctused = ($tot / $disksize[0]) * 100;

Php executing a bash script with at command

Hello I'am trying to execute a shell command in my php script but it is not working.
My php script :
//I save the Order
$holdedOrder->save();
$id = $holdedOrder->id;
$old_path = getcwd();
chdir(__DIR__.'/../');
$scriptFile = 'anacron_job_unhold_order.sh';
$bool = file_exists($scriptFile);
//$bool is true !
//this command works in shell but not in here do not know why
$s = shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when");
chdir($old_path);
return [$s,$bool];
$when has a valid value 4 hours or 4 days ...
The command will be :
echo bash anacron_job_unhold_order.sh 29 | at now +1 minutes
the output is null. Trying it with exec() is returning 127 code
Edit :
I removed www-data from /etc/at.deny and still the same problem
The command shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when"); is probably causing the issue, you should take a closer look on how at works.
The command should be something like
at [options] [job...]
To give a job to at from the command instead of STDIN, use heredoc syntax
at now + 1 minute <<< 'touch /tmp/test'
So the PHP code should be something like;
$s = shell_exec("/usr/bin/at now + {$when} <<< '/usr/bin/bash {$scriptFile} {$id}'");
exec($s, $output, $return_var);

send output of a shell command launched from script to a log file

I have a php page that creates a shell script that the same php page starts after creating it, inside I have one of many commands that I want to send the process to a log that does not work while others actually work....
<php
$scriptfile = script.sh;
$logfile = process.log;
$imgfile = image.ppm;
//this one works, it sends the output to the log file
$cmd ="Scripts/convert.sh file.doc > $logfile \\\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
//this one does not work, it does not send the output to the log file and stops the process
$cmd = "&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page \$i; tesseract $imgfile-\$i.ppm $imgfile-\$i -l eng; done >> $logfile";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
$cmd = "/bin/sh $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
?>
I have tried the not working command from the shell and it does send the output to the log file, either this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-$i.ppm image-\$i -l eng; done >> process.log
or this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i >> process.log; tesseract image-$i.ppm image-\$i -l eng; done
And here you have the way the shell script looks like after being created by php:
#! /bin/sh
Scripts/convert.sh file.doc >> process.log \
&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-000.ppm image-$i -l eng; done >> process.log
So my question is, what can be wrong, I've tried many different things already but no success unfortunately, any help or advice will be very welcomed!! thanks from now!!

List highest number inside all text files (multiple text files in directory)

I have a directory with multiple text files.
For example:
name1.txt
name2.txt
name3.txt
etc.. etc..
Each text file holds 1 line, on that line is a number i.e "10"
I was wondering if it'd be possible to somehow echo back the text file names of say the top 10 highest numbers of all the text files.
I was wondering if this could be done live via PHP or updated periodically via a bash script / cron
Thanks!
Not the most efficient idea but assuming you can't use a DB (otherwise you probably would):
<?php
$files = scandir('path/to/files/directory');
$rows = Array();
foreach($files as $file){
array_push($rows,file_get_contents('path/to/files/directory/'.$file);
}
arsort($rows);
$i = 0;
foreach($rows as $key => $row){
if($i <= 10){
echo 'On '.$files[$key].' the number is'.$row;
}
}
?>
grep . name*.txt | sort -nr -k2 | head -n 3
Output (e.g.):
name4.txt:1
name3.txt:123
name2.txt:444
With bash.
First, create some files:
for n in $(seq 20); do echo $RANDOM > name${n}.txt; done
Now, top 5:
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5
name16.txt 30283
name12.txt 29976
name8.txt 28948
name4.txt 28256
name6.txt 28148
Just the filenames
$ for f in *; do printf "%s\t%d\n" "$f" $(<$f); done | sort -k2nr | head -n 5 | cut -f1
name16.txt
name12.txt
name8.txt
name4.txt
name6.txt

Use FFMPEG to get frame from a video on specific time (PHP)

Using FFMPEG, I want to get the frame from the video on a specific time (provided by the user) in PHP.
Can anybody help me with this? I've never used FFMPEG.
Thanks in advance.
I found the solution
//video dir
$video = 'Salient.avi';
//where to save the image
$image = 'testimage%d.jpg';
//time to take screenshot at
$interval = '00:00:12';
//screenshot size
$size = '535x346';
echo "Starting ffmpeg...\n\n<br>";
$cmd = "ffmpeg -i $video -ss $interval -f image2 -s $size -vframes 1 $image";
echo shell_exec($cmd);
echo "Done.\n";

Categories