Skip to content

Instantly share code, notes, and snippets.

@tobyokeke
Last active November 24, 2020 05:37
Show Gist options
  • Save tobyokeke/b8d575d61e059a70a5a42c047896f8e7 to your computer and use it in GitHub Desktop.
Save tobyokeke/b8d575d61e059a70a5a42c047896f8e7 to your computer and use it in GitHub Desktop.
Process Video using FFMPEG in Laravel
public function addVideo(Request $request) {
$video = $request->file('video');
$user = $request->user();
$mainFileName = Str::random(6) . now()->timestamp . "." . $video->getClientOriginalExtension();
$previewFileName = Str::random(6) . now()->timestamp . ".mp4";
$mainFilePath = 'uploads/' . $user->uid .'/video/'. $mainFileName;
$previewFilePath = 'uploads/' . $user->uid .'/video/'. $previewFileName;
$overlayedFilePath = "uploads/" . $user->uid . "/video/overlayed_" . $video->id . ".mp4";
$watermarkFilePath = 'watermark.png';
$picName = 'uploads/' . $user->uid .'/video/preview.jpg';
//save locally
Storage::disk('public')->put($mainFilePath, file_get_contents($video));
// this would convert the video to a 480p mp4 video
exec("ffmpeg -i '$mainFilePath' -s hd480 '$previewFilePath' ");
// this would give you a screenshot at the 5th second. Change the number after
// -ss to choose the second of the video you want the pic to be taken at
exec("ffmpeg -y -i '$mainFilePath' -ss 5 -vframes 1 $picName");
while(!file_exists($previewFilePath)){
sleep(1);
}
// this would add a watermark on the video to the bottom left
exec("ffmpeg -i 'temp/$previewFilePath' -i '$watermarkFilePath' \-filter_complex \"[0:v][1:v] overlay=W-w:H-h:enable='between(t,0,20)'\" \-pix_fmt yuv420p -c:a copy '$overlayedFilePath'");
// do any further processing
return response( array( "message" => "Video Uploaded.", "data" => [
"video_file" => $mainFilePath,
"video_preview_file" = $previewFilePath,
"video_pic_file" => $picName
] ), 200 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment