How do i loop thru video files in a directory with extension .flv and use exec to convert each one of them (one at a time ) to mpg using ffmpeg -i original.avi final.mpg
While current file is converting, the next filename in the array should wait until exec process is complete before moving on and converting other filename in the array.
I know i can 开发者_如何学Gouse scandir($dir)
to make an array of all filenames, then maybe use split to only target .flv
files. Is this the best way to do it. If i use a while loop to run thru each filename and do an exec on it, how do i make sure exec file is fully converted before moving on to the next file in the array.
$dir='/path/to/dir';
$bash_commands = 'cd "'. $dir .'"
for i in *.flv; do
OUTFILE=$(basename "$i" .flv).mpg
ffmpeg -i "$i" -f mpg "$OUTFILE"
done';
exec($bash_commands);
BASH code explanation:
- The first line changes the current working directory with the command
cd
.
Refer: Moving around the filesystem @ tldp.org - The second line starts a for loop that repeats the code inside it, while the
$i
variable contains the name of a file from the current working directory that is of the following pattern:*.flv
. Each iteration changes the value of$i
to the next file name in the current working directory.
Refer: The for loop @ tldp.org and Loops @ tldp.org. - The third line assigns the value
$(basename "$i" .flv).mpg
to the variable namedOUTFILE
.
Refer: Subshells @ tldp.org, basename @ tlpd.org and Command Substitution @ tlpd.org. - The fourth line initiates ffmpeg and tells it to convert the file in
$i
to a file named like$i
, but with it's suffix changed to "mpg" instead of "flv". (The "suffix change" is done in the third line) - The fifth line ends the for loop.
The following should be the PHP equivalent:
chdir('/path/to/current/working/dir');
foreach (glob("*.flv") as $filename) {
$OUTFILE = substr($filename, 0, -3) . 'mpg';
exec('ffmpeg -i "'.$filename.'" -f mpg "'.$OUTFILE.'"');
}
Refer: glob(), chdir() and substr().
精彩评论