You need to create a thumbnail to download the video (very simple code)

I have this page (it’s very simple to show what I need) to upload FLV files - I read some messages about ffmpeg-php, but how to install it on the server if this solution and how to use it?

<?php if(isset($_REQUEST['upload'])){ $tmp_name = $_FILES['video']['tmp_name']; $name = $_FILES['video']['name']; $path = "videos/"; move_uploaded_file($tmp_name,$path.$name); } else{ ?> <form action="" method="post" enctype="multipart/form-data"> <input name="video" type="file" /> <input name="upload" type="submit" value="upload" /> </form> <?php } ?> 

and need to create a thumbnail for a video uploaded to another folder with the same name any help? thanks in advance

+6
source share
2 answers

Installing ffmpeg should be easy. On any Ubuntu / Debian based distribution, use apt-get:

 apt-get install ffmpeg 

After that, you can use it to create a thumbnail.

First you need to get a random time location from your file:

 $video = $path . escapeshellcmd($_FILES['video']['name']); $cmd = "ffmpeg -i $video 2>&1"; $second = 1; if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) { $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4]; $second = rand(1, ($total - 1)); } 

Now that your $second variable is set. Get the actual thumbnail:

 $image = 'thumbnails/random_name.jpg'; $cmd = "ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1"; $do = `$cmd`; 

It will automatically save the thumbnail to thumbnails/random_name.jpg (you can change this name based on the downloaded video)

If you want to resize the thumbnail, use the -s ( -s 300x300 )

See the ffmpeg documentation for a complete list of options you can use.

+9
source

Or you can do it in a browser with HTML5 tags and a canvas, see https://gist.github.com/adamjimenez/5917897

+3
source

Source: https://habr.com/ru/post/907149/


All Articles