Join two flv files with PHP and ffmpeg - php

On my website I'm using phpmotion to convert videos into FLV files.
What I want to do is that after the successful conversion of any new FLV file add short FLV file at the beginning.
So, I need FFMPEG command in PHP which will join the file 1.flv (intro file) with 2.flv (successful converted file) and as a result create final.flv
I tried with:
ffmpeg -i 1.flv -i 2.flv -vcodec copy -acodec copy final.flv
But without result.
Thanks for any suggestion.

Here is the code, you have to seperate audio and video to raw files at first, join them then again convert back to flv
mkfifo temp1.a
mkfifo temp1.v
mkfifo temp2.a
mkfifo temp2.v
mkfifo all.a
mkfifo all.v
ffmpeg -i 1.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp1.a < /dev/null &
ffmpeg -i 2.flv -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 - > temp2.a < /dev/null &
ffmpeg -i 1.flv -an -f yuv4mpegpipe - > temp1.v < /dev/null &
{ ffmpeg -i 2.flv -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp2.v ; } &
cat temp1.a temp2.a > all.a &
cat temp1.v temp2.v > all.v &
ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i all.a \
-f yuv4mpegpipe -i all.v \
-sameq -y output.flv
rm temp[12].[av] all.[av]

I guess you can use mencoder to merge two files.
mencoder -oac copy -ovc copy -o c:\video.flv c:\a.flv c:\b.flv

Related

FFMPEG command in PHP shell_exec is too slow for a 300Mo mp4

I have a web site where user upload videos.
When a video is uploaded I shell_exec 3 ffmpeg commands:
Generate preview:
$cmd = "ffmpeg -ss 1 -i ".$filePath." -vf \"scale='if(gt(dar,360/202),202*dar,360)':'if(gt(dar,360/202),202,360/dar)',setsar=1,crop=360:202\" -frames:v 1 ".$thumbnail;
Compress the video:
$cmd = "ffmpeg -i ".$filePath." -c:v libx264 -crf 23 -preset veryfast -c:a aac -ab 128k -movflags faststart ".$wmoutput;
Add watermark:
$cmd = 'ffmpeg -i '.$wmoutput.' -i new/watermark.png -filter_complex "overlay=10:10" '.$finaloutput;
The problem is that these 3 commands take a lot of time to finish and sometimes it goes over Maximum execution time specialy for video files that are bigger than 300Mo. I can just expand the Maximum execution time but I don't know how many seconds I have to set.
Is there a way to make the scripts above execute faster without having to change the Maximum execution time?
Combine Compress the video and add watermark into one command:
ffmpeg -i input.mp4 -i new/watermark.png -filter_complex "overlay=10:10" -c:v libx264 -crf 23 -preset veryfast -c:a aac -ab 128k -movflags faststart output.mp4
Should be significantly faster and you avoid yet another instance of generation loss.

Exec through PHP. I cant get the output (stderr) into a file

When I run exec() through php, I fail to get the output (stderr) into a file.
I've included "2> my_out_put_file.txt" at the end of my command to accomplish this. But when I include this, the command is not executed. However, if I run the command without "2> my_out_put_file.txt" then it works.
The interesting thing is that the whole command, even with "2> my_out_put_file.txt" at the end of the command works if I run directly via shell/promt, but not when I run it through php / apache.
Does not work:
exec("C:/FFmpeg/bin/ffmpeg -i $new_path/$filename_with_ext -f mp4 -vcodec libx264 -preset ultrafast -profile:v main -acodec aac $new_path/$filename.mp4 2> out.txt", $a, $b);
Works:
exec("C:/FFmpeg/bin/ffmpeg -i $new_path/$filename_with_ext -f mp4 -vcodec libx264 -preset ultrafast -profile:v main -acodec aac $new_path/$filename.mp4", $a, $b);
Best regardsNeo
This worked for me:
... 2>&1 >> log.txt
Tested from exec too:
php -r "exec('ffmpeg -version 2>&1 >> log.txt');"

what is the proper way for text in multi-pass encoding using ffmpeg

cpulimit -l 180 ffmpeg-static/ffmpeg -ss 0 -t 8.96 -i '/var/www/html/Videos2/Character Jack/Happy/happy birthday scene.mp4' -filter_complex '[0] drawtext=textfile=scene2.txt:fontsize=199.68:fontfile=/var/www/html/fonts/Arial.ttf:x=288:y=409:fontcolor_expr=ffffff%{eif\: clip(1+(255*t/4)\, 0\, 255)\:x\:2}' -pass 1 -f h264 -y - > /dev/null &&
cpulimit -l 180 ffmpeg-static/ffmpeg -ss 0 -t 8.96 -i '/var/www/html/Videos2/Character Jack/Happy/happy birthday scene.mp4' -i '/var/www/html/Userfiles/Users/blaze/Projects/earl3x/audio/scene2_music.x.mp3' -filter_complex '[1] volume=0.95 [au1]; [0][au1] amix=inputs=2:duration=shortest' -c:v copy -c:a libmp3lame -pix_fmt yuv420p -preset ultrafast -shortest -movflags faststart -y -pass 2 /var/www/html/Userfiles/Users/blaze/example/earl4x/scene2.mp4
What i'm trying to achieve ?
encoding text elements / graphics on the first pass
and encoding Sound (audio) on the second pass
the first pass and second pass runs fine ( with no errors at all)
and the output has no artifact.
however the text is totally no where to be found!
what it seems like is that , the second pass had no idea there was a first pass with text encoded in it.
what will be the right way to run / format the command ?
thanks
i solved this.
i had to go through another method
-generate the video with with draw text ( dont parse the -pass 1 parameter)
just run your output normally
-put you output url as the input url of the next command (use -y and remember no passes)
add the audio file as the second input parameter
-output a final file
note things could get really muddy if you have a series of complex combinations
so i have to figure out an algorithm specific to my case that worked.
The drawtext has to be applied both times. What pass 1 is generate the video, analyze video and then discard it, so drawtext has to be present for pass 2 as well.
ffmpeg -ss 0 -t 8.96 -i '/var/www/html/Videos2/Character Jack/Happy/happy birthday scene.mp4' \
-filter_complex \
'[0] drawtext=textfile=scene2.txt:fontsize=199.68:fontfile=/var/www/html/fonts/Arial.ttf: \
x=288:y=409: \
fontcolor_expr=ffffff%{eif\: clip(1+(255*t/4)\, 0\, 255)\:x\:2}' \
-pass 1 -c:v libx264 -f null -
2nd pass:
ffmpeg -ss 0 -t 8.96 -i '/var/www/html/Videos2/Character Jack/Happy/happy birthday scene.mp4' \
-i '/var/www/html/Userfiles/Users/blaze/Projects/earl3x/audio/scene2_music.x.mp3' \
-filter_complex \
'[0] drawtext=textfile=scene2.txt:fontsize=199.68:fontfile=/var/www/html/fonts/Arial.ttf: \
x=288:y=409: \
fontcolor_expr=ffffff%{eif\: clip(1+(255*t/4)\, 0\, 255)\:x\:2}[v];
[1]volume=0.95 [au1]; [0][au1] amix=inputs=2:duration=shortest[a]' -map "[v]" -map "[a]" \
-pass 2 -c:v libx264 -c:a libmp3lame -pix_fmt yuv420p -preset ultrafast -shortest \
-movflags +faststart -y \
/var/www/html/Userfiles/Users/blaze/example/earl4x/scene2.mp4

executing exec in cakephp for ffmpeg not converting video

I am trying to run this in cakephp
function test()
{
$mainDirectoryPath = WWW_ROOT . "media";
$filePath = "userId_10/vid_1444387525.mp4";
$newPath = "userId_10/Output.mp4";
$value = exec("ffmpeg -i $filePath -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
$this->jsonOutput($value);
}
i am getting nothing $value is empty,
however in shell if i run this as shell_exec() in a php file it works.
what is wrong above
UPDATE:
Ok now i have created a file with following code
<?php
$mainFile = $_REQUEST['mainFile'];
$newPath = $_REQUEST['newFile'];
echo shell_exec("ffmpeg -i $mainFile -s 320x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 2 -ar 44100 -ab 128k $newPath");
#unlink($mainFile);
?>
If i run this with mainFile and newFile it works.
However when i run it in function as
$val = "php video.php?mainFile=$filePath&newFile=$newPath > /dev/null 2>&1 &";
exec($val);
Nothing happens.
Check php.ini file and disable_functions have not exec comamnd.
For security reason site admin disabled exec command in php.ini.
Remove exec from disable_functions in php.ini file.

Error while Creating Videos from images using ffmpeg

I am trying to create videos from images sequence. I tried all the below code given in $ff_command variable.
$path = dirname(__FILE__);
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg video.mpg";
$ff_command = "ffmpeg -r 10 -b 1800 -i {$path}/img%03d.jpg test1800.mpg";
$ff_command = "ffmpeg -i {$path}/img%03d.jpg -s 1280x720 -aspect 16:9 -r 24 -vb 20M teste.mp4";
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg -vcodec mpeg4 -b 800k video.avi";
$ff_command = "ffmpeg -f image2 -i {$path}/img%03d.jpg -vcodec libx264 -b 800k video.avi";
Most of these commands are creating a video file. But when I open that video nothing comes or first image is coming for a split second and nothing after that
Please let me know what I am doing wrong here
I noticed that video is too short to see. Is it possible to increase the video duration so that we can see all the images?

Categories