简体   繁体   中英

How to optimize FFMPEG/ Editing video?

I have the next commands for editing video but all the process take a long time. But with the same quality of the original video.

//First cut original video
exec("ffmpeg -i $video_path_main -ss $first_time1 -t $first_time2 -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 1 -strict -2 $name_first");
exec("ffmpeg -i $video_path_main -ss $second_time1 -t $second_time2 -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 1 -strict -2 $name_second");

$name_edit_second = uniqid() . '.mp4'; //Then editing the second video
exec("ffmpeg -i $name_second -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 1 -strict -2 -vf movie='" . $image_name . " [watermark]; [in] [watermark] overlay=308:43"."' $name_edit_second");

//Then merge video file mp4 with Mencoder
$name_total_1 = uniqid() . '.mp4';
exec("mencoder -oac pcm -ovc xvid -vf scale -xvidencopts bitrate=460 -o $name_total_1 ".$name_first.' '.$name_edit_second);

//Then convert the video to 3 formats that is necessary in my Player.
$name_total = uniqid();

//Of MP4 a FLV
exec("ffmpeg -i $name_partial -f flv -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 1 $name_total.flv");

//Of MP4-Mencoder a MP4-FFMPEG
exec("ffmpeg -i $name_partial -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 1 -strict -2 $name_total.mp4"));

//Of MP4 a WEBM
exec("ffmpeg -i $name_partial -acodec libvorbis -s 476x268 -r 10 -b 2000k -r 30 -g 100 -ar 22050 -ab 48000 -ac 2 -f webm $name_total.webm");

I don't know if some of parameters take much time for all the process. Or if one of this command take much time.

Note: Some videos have more than 2 parts of their original videos.


UPDATE

Maybe the parameter -theards 1 help me in NO take a lot of resources of the CPU. Also, I need to optimize the re-encoding because with only 8 users take the 100% of resources.

I run FFMPEG in a other server that return the video edited to other server where stay my application.

Sorry for my english.

Make segments and overlay image

ffmpeg -i input.flv -i image.jpg -ss 30 -t 5 -c:v libx264 -preset medium \
-crf 23 -filter_complex overlay=308:43 -c:a libfaac -q:a 100 output1.mp4

ffmpeg -i input.flv -i image.jpg -ss 60 -t 5 -c:v libx264 -preset medium \
-crf 23 -filter_complex overlay=308:43 -c:a libfaac -q:a 100 output2.mp4

Concatenate the segments and encode

First make the file list that the demuxer will read from. It is named list.txt in this example:

echo "file 'output1.mp4'" >> list.txt
echo "file 'output2.mp4'" >> list.txt

The contents of list.txt are simply:

file 'output1.mp4'
file 'output2.mp4'

Now concatenate the videos output1.mp4 and output2.mp4 using the concat demuxer . The demuxer will use the files listed in list.txt as inputs:

ffmpeg -f concat -i list.txt -c copy -movflags faststart final.mp4
ffmpeg -f concat -i list.txt -c:v libvpx -c:a libtheora -q:a 3 final.webm

The -movflags faststart will allow the mp4 file to begin playback in JW Player before it is completely downloaded. You will want to add some sort of rate control method to the webm example (such as -b:v ). I am unfamiliar with this encoder and the defaults are not great.

Now you have a file with H.264 video and AAC audio in MP4 container, and a file with VP8 video and Vorbis audio in webm container which should provide decent coverage for the various browsers.

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM