Upload and Play Any Video in web browser using php CI - php

I have tried to upload any video and convert the video file to mp4 at the time of upload using ffmpeg. The shell command I tried:
$input = base_url().'video_upload/'.$file_name;
$output = video_upload/video_'.$video_data['username'].'_'.$dtd.'_'.$random_number.'.mp4';
$convt = shell_exec('ffmpeg -i '.$input.' '.$output);
But i am unable to play those converted video in the web browser.

Related

How to open a remote video using PHP-FFMpeg?

I'm trying to create a web application using PHP (Laravel 5.8) where user can past link of any video found on the Internet (like Youtube, Dailymotion ... etc), then cut the video.
Cutting the video the video both in front-end and back-end is easy to do, I'm using PHP-FFMPeg to do it in server side.
My problem is that I couldn't find a solution to open a remote video from my PHP script, i.e if user past this link "https://www.dailymotion.com/video/x6rh0" I would open it then make a clip.
This my code :
$ffmpeg = FFMpeg\FFMpeg::create();
$url = "https://www.dailymotion.com/video/x6rh0";
$video = $ffmpeg->open($url);
$clip = $video->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(15));
$clip->save(new FFMpeg\Format\Video\X264('libmp3lame', 'libx264'), public_path().'/videos/video2.avi');
I'm using PHP Laravel framework.
Please, how can I open a video from URL using PHP-FFMpeg, this is my question.

How to get video size of compressed video by ffmpeg?

Hi we are using ffmpeg for compressing the video through php script, now what i need is i want to get the video size of compressed image, but am getting an video path so kindly guide me how i need to over come this?
Below is the code what i used for compressing the video
original path
$path = "sample.mp4";
Command to compress
exec("ffmpeg -i sample.mp4 -vcodec h264 -acodec aac -strict -2 compressed_video.mp4);
The command what i used for getting video size
$compressed_video_information = exec("ls -h1 compressed_video.mp4);
echo $compressed_video_information;
I get just file path instaed of getting video file size, so someone help me how to overcome this issue?
-h option (ex: ls -lh) displays size in human readable form(KB/MB/GB etc..)
exec("ls -lh compressed_video.mp4",$out);// pass file path here
$size=explode(' ',$out[0]);
print_r($size[4]);
Use the PHP function filesize:
<?php
$filesize_in_bytes = filesize('compressed_video.mp4');

different video format supported by html and major browsers

hello all i am building a video hosting site and wanted to know what are the video formats which my php code should convert the user's video so that all the major browsers support the file.
by searching on internet i got to know that mp4,swf,avi,ogg are the formats but if i convert one video to all these formats then there will be 4 times the space of a single video and also there will be server load and time consuming process for the conversion.
so i was wondering if there is one or two formats which i should take with me so to reduce the server load and conversion time.
i am converting the video by this code (ffmpeg)
if (move_uploaded_file(#$_FILES['profileimage99']['tmp_name'], $uploadfile)) {
$base = basename($uploadfile, $safe_file['ext']);
$new_file = $base.'flv';
$new_image = $base.'jpg';
$new_image_path = $live_img.$new_image;
$new_flv = $live_dir.$new_file;
require 'vendor/autoload.php';
//ececute ffmpeg generate flv
exec('ffmpeg -i '.$uploadfile.' -f flv -s 768x432 '.$new_flv.'');
//execute ffmpeg and create thumb
exec('ffmpeg -i '.$uploadfile.' -f mjpeg -vframes 71 -s 768x432 -an '.$new_image_path.'');
also please tell me will it be good to use exec in php code ??
The most common web video formats are WebM, OGG, and MP4 with MP4 being supported in IE, Chrome, Safari, Opera, and FireFox.
Therefore, it would be prudent to stick with MP4.

Upload a video via PHP and encode to MP4 and/or WebM on the server?

I want to upload a file (Indeed, I already did it using PHP and JQuery) but I want to encode it to MP4 and/or WebM in the process of the upload, such as Youtube does when you upload a video there. Is there a option to be able to do it in the server during the process?
Do I have to encode them first and then upload?
You can do it at the end of file upload, which is after you moved the file to a specific location (below code)
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
You can use a free and well-known library called FFMPEG which supports a wide range of formats. Please take a look at these two links for example and better explanation:
https://www.phpro.org/tutorials/Video-Conversion-With-FFMPEG.html
https://trac.ffmpeg.org/wiki/Using%20FFmpeg%20from%20PHP%20scripts
Basically, you can call the FFMPEG function from PHP like this
<?php
/*** convert video to flash ***/
exec("ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv");
?>

Extract frame from video - PHP-FFMpeg

I am using php ffmpeg in a laravel project, to do multiple things probe, extract frame and encode. I am having an issue when creating a frame from the uploaded video file.
This is how the frame is created:
$video = $ffmpeg->open($destinationPath.'/'.$filename);
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
->save(public_path().$frame_path);
This is sometimes working and creates the frame but other times is not. I noticed that this bug comes up when I am trying to open a .mov file.
It's possible that your version of ffmpeg does not support the codec that is used in the source video file, and hence it is not able to decompress the video and extract an image.
You could try processing the file from the command line to see if you can extract an image that way, and ffmpeg may give you some more information on the problem.
An example command line to extract a png frame from a video file
ffmpeg -y -ss 30 -i [source_file] -vframes 1 [target_file]
Add -f image2 as an output option if your output name is a variable.
The PHP-FFMpeg library appends the -ss argument by default before the input file which requires the timestamp to be accurate in order to obtain the frame. I encountered this problem in case of mkv file. Files such as mkv and mov can not be accurately seeked.
https://github.com/PHP-FFMpeg/PHP-FFMpeg/blob/master/src/FFMpeg/Media/Frame.php#L79
You need to pass true as the second argument to the save function in order to give a Frame closest to the given point. It changes the position of -ss argument in ffmpeg command.
-ss position (input/output)
When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is
not possible to seek exactly, so ffmpeg will seek to the closest seek
point before position. When transcoding and -accurate_seek is enabled
(the default), this extra segment between the seek point and position
will be decoded and discarded. When doing stream copy or when
-noaccurate_seek is used, it will be preserved.
When used as an output option (before an output filename), decodes but
discards input until the timestamps reach position.
position must be a time duration specification, see (ffmpeg-utils)the
Time duration section in the ffmpeg-utils(1) manual.
Here is the code I've been using with PHP:
https://totaldev.com/extract-image-frame-video-php-ffmpeg/
<?php
// Full path to ffmpeg (make sure this binary has execute permission for PHP)
$ffmpeg = "/full/path/to/ffmpeg";
// Full path to the video file
$videoFile = "/full/path/to/video.mp4";
// Full path to output image file (make sure the containing folder has write permissions!)
$imgOut = "/full/path/to/frame.jpg";
// Number of seconds into the video to extract the frame
$second = 0;
// Setup the command to get the frame image
$cmd = $ffmpeg." -i \"".$videoFile."\" -an -ss ".$second.".001 -y -f mjpeg \"".$imgOut."\" 2>&1";
// Get any feedback from the command
$feedback = `$cmd`;
// Use $imgOut (the extracted frame) however you need to
// ...

Categories