Concatenate two audios with different duration time - php

I need to concatenate two audios, but I need to keep the size of the video with the size of the audio "backgroud.wav". I'm doing like this:
$code1 = "ffmpeg -y -i voz.wav -i backround.wav -filter_complex amerge -c:a libmp3lame -q:a 4 somnovo2.mp3";
system($code1);
But the result is the size of the audio "voz.wav" which would be 30 seconds. While "background.wav" is 3 minutes long. I need the output to have the time of "background.wav". How can I do this ?

Try apad filter before amerge
$code = 'ffmpeg -y -i voz.wav -i background.wav -filter_complex "[0]apad[a];[a][1]amerge[aout]" -map "[aout]" -c:a libmp3lame -q:a 4 output.mp3'
system($code);

As Anwsonwsymous answer, the final result looked like this:
$code = "ffmpeg -y -i voz.wav -i background.wav -filter_complex \"[0]apad[a];[a][1]amerge[aout]\" -map \"[aout]\" -c:a libmp3lame -q:a 4 output.mp3";
$code1 = "ffmpeg -loop 1 -i result.jpg -i output.mp3 -shortest -acodec copy fim.mp4";
system($code);
system($code1);
The
system($code);
merges the two audios into one, keeping the background audio repeating until the end.
End
system($code2);
merges the finished audio with an image and converts it to .mp4 video with the duration of the background audio.

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.

Ideal bitrates for different video resolutions

I am building a Video-on-demand service for a closed community. I using FFMPEG for video processing and dash.js for adaptive bitrate player with custom resolution selector. Can somebody please suggest what ideal bitrates should I use while video/audio transcoding?
I am talking about -b:v and -ab option
ffmpeg -i vid.mp4 -c:v libvpx-vp9 -keyint_min 150 \
-g 150 -tile-columns 4 -frame-parallel 1 -f webm -dash 1 \
-an -vf scale=144:-1 -b:v 120k -dash 1 video_1.webm \
-an -vf scale=240:-1 -b:v 250k -dash 1 video_2.webm \
-an -vf scale=360:-1 -b:v 500k -dash 1 video_3.webm \
-an -vf scale=480:-1 -b:v 750k -dash 1 video_4.webm \
-an -vf scale=720:-1 -b:v 1500k -dash 1 video_5.webm
And
ffmpeg -i vid.mp4 -vn -acodec libvorbis -ab 96k -dash 1 audio_96k.webm
Any suggestions/hacks or examples to tackle real-world network situations are appreciated.
There is no ideal. Every video is different, and every viewer is different. What is ideal for one viewer is not ideal for another. Read the Netflix blog on pert title encoding and vmaf. Also look at Akamai state of the internet reports to determine what average global internet connection speeds are.

ffmpeg command is not run when I hit using exec function in php

I have to convert some video to "h264" using FFmpeg.When I hit the below command as a cloud user with ssh login it converts successful.
ffmpeg -i /var/www/media/photos/video_demo/55291482115655.MP4 -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:300 -threads 0 -pass 1 -codec:a libfdk_aac -b:a 500k -f mp4 /var/www/media/photos/video_demo/5888.MP4.
But when I run this command using PHP it gives me an error.
$cmd = "ffmpeg -i /var/www/media/photos/video_demo/55291482115655.MP4 -codec:v libx264 -profile:v high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -vf scale=-1:300 -threads 0 -pass 1 -codec:a libfdk_aac -b:a 500k -f mp4 /var/www/media/photos/video_demo/85493.MP4 ";
exec($cmd .' 2>&1', $outputAndErrors, $return_value);
php error: Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
I also followed this link similar to my problem but still not get solution for this problem,In my server there is 3 FFmpeg installed.

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

Why does FFMPEG work in terminal but not in php with exec()?

I am trying to get FFMPEG to work in php. I just installed ffmpeg and x264 and ran the following command in my terminal:
$command = 'ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 /home/gman/Desktop/newvideo.flv
It worked perfectly and created a new flv video from the inital video, just like I wanted.
Now when I try the same thing in php, nothing happens...
$safe_path = escapeshellarg("/home/gman/Desktop/newvideo.flv");
$command = 'ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 ' . $safe_path;
exec($command);
Anyone have any ideas? Can I somehow see what exec is doing and see some sort of output? Would appreciate it.
Usually when you are calling ffmpeg in an exec you need to put in the absolute path to ffmpeg eg:
$safe_path = escapeshellarg("/home/gman/Desktop/newvideo.flv");
$command = '/usr/local/bin/ffmpeg -i /home/gman/Desktop/cave.wmv -acodec libfaac -aq 100 -vcodec libx264 -preset slow -crf 22 -threads 0 ' . $safe_path;
exec($command);
http://blog.codyjung.com/2011/05/29/problems-with-lampp-and-exec/
My solution was to simply copy the ones from /usr/lib/i386-linux-gnu and overwrite the LAMPP ones. Could that cause problems later? Maybe, but I guess we’ll deal with that when it shows up.

Categories