Add overlay or watermark on video – PHP & FFmpeg

In this article, we will be using FFmpeg tool with PHP to add watermark on a video.

Watermark on video

Watermark (also known as overlay image) is an image that displays in a specific position throughout the video. It is important to display your brand logo along with your video. Also it serves as a material that this content has been provided by your brand. If you have ever used Microsoft Word, then you would probably be aware of what watermark is. In MS Word, you can display watermark in diagonal or horizontal format. That type of watermarks are used for documents only.

In terms of video, you cannot display this on whole video. So you need an image of lower resolution then your video so it can be placed in somewhere inside the video. Also, you need to position the overlay image in a proper location. The position of image should be so that user must not get distracted from your actual video. The recommended positions are either top left, right or bottom left or right.

FFmpeg

You need to download and install FFmpeg in your system. After installation, you can also add it in your environmental PATH variable. We are going to create a PHP script that will input 2 files from user, one video where watermark should be placed and second is the overlay image which will be placed throughout the video.

Creating layout

Create a file named index.php in your htdocs or www folder if you are using XAMPP or WAMP respectively. If you already have a live site with domain and hosting, you need to connect with your server using SSH and run all the commands at the root of your project. Also mare sure you have set the folder permission to read and write, otherwise FFmpeg will not be able to create a new video with overlay image. Paste the following code in index.php file:

<link rel="stylesheet" type="text/css" href="bootstrap.min.css">

<div class="container" style="margin-top: 100px;">
	<div class="row">
		<div class="offset-md-3">
			
			<!-- creating the form -->
			<!-- apply encoding type to process file -->
			<form method="POST" action="add-overlay.php" enctype="multipart/form-data">
				
				<!-- creating video input file -->
				<div class="form-group">
					<label>Select video</label>
					<input type="file" name="video" class="form-control">
				</div>

				<!-- creating image input file -->
				<div class="form-group">
					<label>Select image</label>
					<input type="file" name="image" class="form-control">
				</div>

				<!-- create submit button -->
				<input type="submit" class="btn btn-primary" value="Add overlay">

			</form>

		</div>
	</div>
</div>

Add overlay image to video

Now we need to process the form i.e. to add selected image as overlay to selected video file and save the output as new file with same video and audio quality. The process involves 2 steps, first, as the selected image could be a very high quality image with very high resolution. So we need to resize the image to resolution without decreasing the quality. Secondly we will apply that resized image as overlay to video. Create a new file named “add-overlay.php” or same as action attribute in <form> tag. Paste the following code in newly created add-overlay.php file:

<?php

// first you have to get both input files in separate variables
$video = $_FILES["video"]["name"];
$image = $_FILES["image"]["name"];

// then you have to resize the selected image to lower resolution
$command = "/usr/local/bin/ffmpeg -i " . $image . " -s 128x128 output.jpeg";

// execute that command
system($command);

echo "Overlay has been resized";

// both input files has been selected
$command = "/usr/local/bin/ffmpeg -i " . $video . " -i output.jpeg";

// now apply the filter to select both files
// it must enclose in double quotes
// [0:v] means first input which is video
// [1:v] means second input which is resized image
$command .= " -filter_complex \"[0:v][1:v]";

// now we need to tell the position of overlay in video
$command .= " overlay=80:50\""; // closing double quotes

// save in a separate output file
$command .= " -c:a copy output.mp4";

// execute the command
system($command);

echo "Overlay has been added";

?>

Explore what more you can do with FFmpeg.

Generate thumbnails of video – PHP & FFmpeg

In this tutorial, we will be creating a script in PHP and FFmpeg. We will provide a video to our script. And our script will generate thumbnails of that video after regular intervals.

Thumbnails of video

Thumbnail is just a preview of a video so user can see what’s inside the video before playing it. You can show single image as a thumbnail, or you can show a GIF image which is a series of multiple frames. The video which has a GIF thumbnail allows you to hover your mouse over the video to play the GIF. The first frame will be seen as a static image. You can generate a list of thumbnails from any video or you can create just one.

Get single thumbnail

If you wanted to get just 1 image from video (also known as screen capture) you need to tell the time duration from where to capture the screen. For example, you need to tell the time in this format (hh:mm:ss). But if you wanted to generate multiple images of video, like YouTube and Facebook does, you need to tell the frame interval. You can tell to capture image every 60 seconds, for example. When you upload any video on YouTube, by default it generates 3 images from the video. Similarly Facebook generates 10 images when you upload the video.

What we are going to do in this post ?

In this tutorial, we will be creating a similar script that will generate thumbnails from video and save them as separate files. In order to generate thumbnails from video, you must have FFmpeg installed in your system. You can download it from it’s official site. We are creating a form that will allow you to select video whose thumbnail needs to be generated.

Creating layout

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 200px;">
	<div class="offset-md-4 col-md-4">
		<form method="POST" enctype="multipart/form-data" action="generate-thumbnail.php">
			<div class="form-group">
				<label>Select video</label>
				<input type="file" name="video" accept="video/*" class="form-control" required>
			</div>

			<input type="submit" class="btn btn-primary" value="Generate">
		</form>
	</div>
</div>

Generating thumbnails of video

At this point if you run the script, you will see a form with 1 input field which allows you to select the video whose thumbnails needs to be generated and a submit button. Now you need to create a new file named “generate-thumbnail.php” and paste the following code in it:

<?php

$video = $_FILES["video"]["name"];

$command = "/usr/local/bin/ffmpeg -i " . $video . " -vf fps=1/60 thumbnail-%03d.png";
system($command);

echo "Thumbnail has been generated";

Now you can run the script and you will see a form with an input field from where you will be able to select the video from your computer. After selecting the video file when you hit the submit button, you will see several new image files will be created at the root of your project’s folder with a prefix thumbnail-“. The time required to process the video and generate thumbnails depends on the length and quality of video. The videos of larger length and of high quality will take longer to process than the one in shorter length.

php.ini

When uploading large files, php.ini plays an important role either you are working on localhost or on live server. Mostly it is located in your XAMPP “etc” folder if you are using Linux or Mac, or if your site is live on server. If you are using Windows OS, then it will be in your XAMPP “php” folder. You may need to change some of the settings to allow the user to upload larger files. Search “upload_max_filesize=128M” in your php.ini file and change it’s value to 512M or 1GB or any other size you want. In same php.ini file, you may need to search for “post_max_size=128M” and increase it’s value too.

That’s all for now. I have also attached all the source files below, make sure you download them and let us know if you face any issue.

There is more in PHP & FFmpeg

Add thumbnail to video – PHP & FFmpeg

[wpdm_package id=’160′]

Add thumbnail to video – PHP & FFmpeg

Thumbnail is basically a preview of video. Every operating system by default add a thumbnail to a video by itself. When you open YouTube, every video you must have see an image along with it’s title. It gives the preview of video and tells what the video is about. Most people use custom images as thumbnail and most are automatically generated by YouTube.

Default video thumbnail

When it comes to operating system, you have a lot of video files in your hard drive and you can see little images that gives the insight of each video. Thumbnail of those videos are automatically generated by operating system itself. The thumbnail assign by OS comes from one of the frames inside the video. Which means the image you see on video thumbnail must have appeared somewhere in the video. “You can change it”

In order to change that thumbnail, you must have FFmpeg installed in your system. You can download it from it’s official site. We are creating a form that will allow you to select video whose thumbnail needs to be changed and image which will be used as new thumbnail.

Creating layout

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 100px;">
	<div class="col-md-6 offset-md-4">
		<form method="POST" enctype="multipart/form-data" action="add-thumbnail.php">

			<div class="form-group">
				<label>Select video</label>
				<input type="file" name="video" accept="video/*" required class="form-control">
			</div>

			<div class="form-group">
				<label>Select image</label>
				<input type="file" name="image" accept="image/*" required class="form-control">
			</div>

			<input type="submit" class="btn btn-primary" value="Add thumbnail">

		</form>
	</div>
</div>

Processing the form

At this point if you run the script, you will see a form with 2 input fields and a submit button. One input field is for selecting video and second is for selecting image. Now you need to create a new file named “add-thumbnail.php” and paste the following code in it:

<?php

$video = $_FILES["video"]["tmp_name"];
$image = $_FILES["image"]["tmp_name"];

$command = "/usr/local/bin/ffmpeg -i $video -i $image -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic output.mp4";
system($command);

echo "Thumbnail has been added";
?>

Now you can run the script and you will see a form with 2 input fields, one for video and one for image. Select the video of your choice and select image from other field. When you hit the submit button, you will see a new file will be created at the root of your project’s folder named “output.mp4”. The newly created video file will have the thumbnail of your selected image file. All other attributes of video (bitrate, resolution, buffer size etc) will remain same.

I have also attached all the source files below, make sure you download them and let us know if you face any issue.

Select thumbnail from video

We created a detailed post on how you can generate thumbnails from a video. After you have generated all the thumbnails, you can add the thumbnail as per your choice.

[wpdm_package id=’158′]

Compress video in bitrate and resolution – PHP & FFmpeg

You can compress the video by bitrate and by resolution in PHP and FFmpeg. You can download FFmpeg from here. Compressing will save you some space in your system.

Compress video in bitrate and resolution – PHP & FFmpeg

When it comes in compressing the video, there are many options which can be adopted. The most basic and effective one is to decrease the bitrate of video. Changing bitrate will result in decreasing the quality of video, thus will reduce the size of video. Decreasing the quality of video will help you in easily transfer the file over the internet. Lower size video will consume less bandwidth and transfer faster than large size videos.

4k

The highest quality video having highest bitrate value is 4K as of writing this today. 4K display also known as the retina display because it gives the clear picture of video as you see with naked eye. Not 3D of course, but talking in terms of quality. Similarly, the least quality video having lowest bitrate value is 144P and it looks good on small screens only. In fact, there are very few devices with such small screen are left today.

So in the near future, this quality most probably be removed at all from the quality format table. Most of the movies you see in Blu-ray are usually in 720P or in 1080P, these are also known as high definition videos HD. As of today, we also have 2K and 4K display but you do not really wanted to compress the video to 2K. Usually you want high quality video to compress to lower quality.

Video’s bitrate and their format

BitrateFormat
350k240p
700k360p
1200k480p
2500k720p
5000k1080p

Another option to compress the video is to change it’s resolution. Resolution in videos is defined by their width and height (w x h), also known as dimension of video. The 4K display usually have the resolution of 2560 x 1600 which you can get in Macbook Pro 2017 and onwards. Decreasing the resolution of video is useful especially when you wanted to view the video in small screens. For example, you have a video in 2560 x 1600 resolution and you wanted to see this in mobile phone. Playing such high resolution video in mobile devices sometimes makes the device run slow, since not all mobile phones have such high processing speeds. Moreover, playing the video in 1200 x 720 resolution is enough to view it in high quality in small screens. There are some phones which can play 1920 x 1080 video seamlessly.

YouTube

When it comes to uploading the video on YouTube, it is recommended to upload the video to at-least 720P which means the video dimension must be at-least 1200 x 720. If you have a video of 720P then YouTube will automatically gives users an option to view the video in lower formats and it also gives your video an HD tag which will be seen on bottom right corner of video thumbnail. So if you have any device that can record 4K video, Macbook Pro or Panasonic or DSLR, you can create the video in 4K but later can reduce the resolution to 1080 or 720P without having the fear to lose the quality of video.

Compress video by bitrate

<link rel="stylesheet" type="text/css" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 200px;">
    <div class="row">
        <div class="col-md-4 offset-md-4">
            <h1>Change bitrate</h1>

            <form method="POST" enctype="multipart/form-data" action="change-bitrate.php">
                <div class="form-group">
                    <label>Select video</label>
                    <input type="file" name="video" class="form-control" required="" accept="video/*">
                </div>

                <div class="form-group">
                    <label>Select bitrate</label>
                    <select name="bitrate" class="form-control">
                        <option value="350k">240p</option>
                        <option value="700k">360p</option>
                        <option value="1200k">480p</option>
                        <option value="2500k">720p</option>
                        <option value="5000k">1080p</option>
                    </select>
                </div>

                <input type="submit" name="change_bitrate" class="btn btn-info" value="Change bitrate">
            </form>
        </div>
    </div>
</div>

Now we need to create a page that will process this request. So create a new file named “change-bitrate.php” and paste the following code in it:

<?php

$video = $_FILES["video"]["tmp_name"];
$bitrate = $_POST["bitrate"];

$command = "/usr/local/bin/ffmpeg -i $video -b:v $bitrate -bufsize $bitrate output.mp4";
system($command);

echo "File has been converted";

Compress video by resolution

<div class="container" style="margin-top: 50px; margin-bottom: 100px;">
	<div class="row">
		<div class="col-md-4 offset-md-4">
			<h1>Change resolution</h1>

			<form method="POST" enctype="multipart/form-data" action="change-resolution.php">
				<div class="form-group">
					<label>Select video</label>
					<input type="file" name="video" class="form-control" required="">
				</div>

				<div class="form-group">
					<label>Select resolution</label>
					<input type="text" name="resolution" class="form-control" placeholder="640x480">
				</div>

				<input type="submit" name="change_resolution" class="btn btn-info" value="Change resolution">
			</form>
		</div>
	</div>
</div>

change-resolution.php

<?php

$video = $_FILES["video"]["tmp_name"];
$resolution = $_POST["resolution"];

$command = "/usr/local/bin/ffmpeg -i $video -s $resolution output2.mp4";
system($command);

echo "File has been converted";

Now you can run the script in your browser and you will be able to change the resolution of selected video file. The original file won’t get affected, there will always be a new file created separately with compressed bitrate or resolution. When you reduce the resolution of video, then it may have a slight effect on video size. For example, 1920 x 1080 video will be compressed to 1280 x 720 but the size of video may have been reduced to just 2 or 3 MB. This is because changing the resolution does not reduct the quality of video, it just convert the high resolution video into low resolution. This is useful when you wanted to see the videos in small screen or when you wanted to share your video on social network.

That’s all for now. I have also attached all the source files below, make sure you download them and let us know if you face any issue.

Save space by splitting video

If you have a video of very large size. And you are having problem in moving it from one hard drive to another. You can split the video in multiple parts. Learn how to do it.

[wpdm_package id=’156′]

Add or extract audio from video – PHP & FFmpeg

In this article, we are going to teach you how you can add audio to a video. And also, how you can extract the audio from the video. We will be using PHP and FFmpeg for both purposes.

Add audio to video

Adding audio to a video will replace the video’s actual audio. You can use it to either replace the video’s audio. Or wanted to put some background music on any of your video. Each video has audio input streams which we can hear when we play the video. If you have ever watched any video that does not have any sound. It means the number of audio steams in that file is 0.

You can add any audio to your video by first removing the audio streams from video file. And then get the audio streams from audio file. And put them on the video file. We will be creating a separate file for output that will contain your video and new audio. Because we do not wanted to make any changes in the original file. You just needs to remember that the length of output file will be equal to the length of maximum media stream.

For example, if audio length is 10 seconds and video length is 15 seconds, the new output will be of 15 seconds. That is why it is a good practice to make the length of both files to be equal. If you have files of unequal lengths, you can always trim the video or audio to required length by following this tutorial. You can also try to keep audio streams from both (audio and video).

You may have seen it in some videos that has the actual sound of the video and they also have a very little, dim music in the background. These type of videos are best for explaining historical facts or world’s cultures etc.

Extract audio from video

Aside from adding music track, sometimes you want to extract an audio. You have a video song in your computer but cannot play that in music player. Because it is a video, you can convert that to audio and then listen that as audio file. As mentioned above, it will not affect your original video file. It will create a separate file with just audio in it. You are watching an action or thriller movie and you want to extract some of music track from it, you can first extract that specific part from video by following this tutorial and then convert that extracted video to audio.

Add music track to video

We will be using bootstrap darkly for layout and design, it will be included in the source files below. Below is the code that will create a simple form with 2 input fields. The first input field will be to select video where music track needs to be added. The second is the mp3 file which will be the music file that needs to be added in the background of selected video.

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 100px;">
    <div class="row">
        <div class="offset-md-4 col-md-4">
            <form method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Select video</label>
                    <input type="file" name="video" class="form-control" required>
                </div>

                <div class="form-group">
                    <label>Select audio</label>
                    <input type="file" name="audio" class="form-control" required>
                </div>

                <input type="submit" class="btn btn-success" value="Add Audio">
            </form>
        </div>
    </div>
</div>

Now we need to process the form data and replace the video’s actual audio with a selected music track. Paste the following code at the top or bottom of your PHP file:

<?php

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $video = $_FILES["video"]["tmp_name"];
        $audio = $_FILES["audio"]["tmp_name"];

        $command = "/usr/local/bin/ffmpeg -i " . $video . " -i " . $audio . " -c:v copy -map 0:v:0 -map 1:a:0 output.mp4";
        system($command);

        $command = "/usr/local/bin/ffmpeg -i " . $video . " original-audio.mp3";
        system($command);
    }

?>

Extracting audio

At this point, we have replaced the video’s actual audio with selected music. Now we wanted to extract the actual audio. This will also be useful if you wanted to convert any video song into mp3 file. This will not only save your memory but also helps in listening the song in your mp3 player or iPod, which otherwise you won’t be able to do. To extract audio from selected video you just needs to run another command right below your system function call.

$command = "/usr/local/bin/ffmpeg -i " . $video . " original-audio.mp3";
system($command);

This is the simplest command you will see related to FF mpeg, after telling the complete path of FF mpeg you just needs to tell the input video file and then the name of output audio file which we are naming as “original-audio.mp3”. This will create a separate mp3 file with the music of selected video. But if you want to convert the audio in some specific format, then you can use the following command:

$command = "/usr/local/bin/ffmpeg -i " . $video . " -b:a 192K -vn original-audio.mp3";
system($command);

This will convert the audio in 192 bitrate format. Change that value to any you want but the basic acceptable formats are 64K, 96K, 128K, 196K and 320K. 320K is the highest and best quality audio format today.

I have also attached all the source files below, make sure you download them and let us know if you face any issue.

[wpdm_package id=’154′]

Merge multiple videos in one file – PHP & FFmpeg

Merge or combining multiple short videos in one long file is usually done by some of the softwares available on the internet. Some operating systems provides basic editing softwares for free, for example Windows 10 has built-in video editor in their Windows Media Player. Macintosh has QuickTime player and iMovie which came along with operating system for free. If you do not have such editor you can always download from the internet and sometimes you have to pay some amount because some of them are not free. In this tutorial, we will be creating a simple PHP script that will help you to combine multiple video and save them in one file as a separate video.

Step 1 – Download FFmpeg

First you need to download and install FFmpeg encoder which you can download from here: Download FFmpeg. This encoder is used to edit video and audio files, you can edit MP4/FLV/MKV/3GP or MOV files. You can edit all types of audio files from this too including MP3, WAV or OGG. FFmpeg is basically a command line tool so after installation try to run the command in your terminal or command prompt:

> ffmpeg

This will make sure FFmpeg has been installed perfectly in your system. Now you can use it’s commands, you can find all documentation in it’s official website.

Step 2 – Creating layout

The design is quite simple, we will be using bootstrap darkly for designing the webpage. Bootstrap darkly will be included in the source files.

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container">
    <div class="row">
        <div class="offset-md-4 col-md-4">
            <form method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <label>Select videos
                    <input type="file" name="videos[]" class="form-control" required multiple>
                </div>

                <input type="submit" name="submit" class="btn btn-primary" value="Merge">
            </form>
        </div>
    </div>
</div>

Step 3 – Merge multiple videos

The next step is to merge multiple videos selected by user. Paste the following code on top of your file including the PHP starting and closing tags:

<?php

if (isset($_POST["submit"]))
{
    $content = "";
    for ($a = 0; $a < count($_FILES["videos"]["name"]); $a++)
    {
        $content .= "file " . $_FILES["videos"]["name"][$a] . "\n";
    }
    file_put_contents("mylist.txt", $content);

    $command = "/usr/local/bin/ffmpeg -f concat -i mylist.txt -c copy output.mp4";
    system($command);
}

?>

Step 4 – Preview of selected videos

At this point, you will be able to select multiple videos and see all of them combined into one named “output.mp4” when the form submits. However, you can do ONE MORE THING….. You can display a preview of each video before submitting the form. To do this, first you have to add an “onchange” listener on input type file, which will be a javascript function that will be called when the user select the videos.

<input type="file" name="videos[]" class="form-control" required multiple onchange="onFileSelected(this);">

This will call the javascript function called “onFileSelected” and send the input type file as a parameter. Now we are going to create a function.

<div id="videos"></div>

Create this div tag where you want to show the preview of videos (may be just below input type file). Make sure to give it a unique ID.

At the bottom of file paste the following code along with script tag at the start and end:

<script>

function onFileSelected(self) {
    var videos = document.getElementById("videos");

    for (var a = 0; a < self.files.length; a++) {
        var file = self.files[a];
        var fileReader = new FileReader();

        fileReader.onload = function (event) {
            var src = event.target.result;
            var newVideo = document.createElement("video");

            newVideo.setAttribute("src", src);
            newVideo.setAttribute("controls", "controls");
            newVideo.setAttribute("width", 500);

            videos.appendChild(newVideo);
        };

        fileReader.readAsDataURL(file);
    }
}

</script>

That’s it, now you can view the preview of all videos before combining them into one. If you wanted to have the source code of this script, you can get it from the button below. Make sure you download it and let us know if you face any issue.

Since you have learned now how to merge multiple videos into one. You can also check our tutorial to split single video file into multiple parts.

[wpdm_package id=’152′]

Split video in multiple parts – PHP & FFmpeg

To split video files in 2 parts or extracting a specific part from a long video is usually done by some of the softwares available on the internet. Some operating systems provides basic editing softwares for free, for example Windows 10 has built-in video editor in their Windows Media Player. Macintosh has QuickTime player and iMovie which came along with operating system for free. If you do not have such editor you can always download from the internet and sometimes you have to pay some amount because some of them are not free. In this tutorial, we will be creating a simple PHP script that will help you to split a video and save the extracted part as a separate video.

Step 1 – Download FFmpeg

First you need to download and install FFmpeg encoder which you can download from here: Download FFmpeg. This encoder is used to edit video and audio files, you can edit MP4/FLV/MKV/3GP or MOV files. You can edit all types of audio files from this too including MP3, WAV or OGG. FFmpeg is basically a command line tool so after installation try to run the command in your terminal or command prompt:

> ffmpeg

This will make sure FFmpeg has been installed perfectly in your system. Now you can use it’s commands, you can find all documentation in it’s official website.

Step 2 – Creating layout

The design is quite simple, we will be using bootstrap darkly for designing the webpage. Bootstrap darkly will be included in the source files.

<link rel="stylesheet" href="bootstrap-darkly.min.css">

<div class="container" style="margin-top: 100px;">
    <div class="row">
	<div class="offset-md-4 col-md-4">
	    <form method="POST" enctype="multipart/form-data">
	        <div class="form-group">
	            <label>Video</label>
       	            <input type="file" name="video" class="form-control">
	        </div>

               <div class="form-group">
                   <label>Cut from</label>
                   <input type="text" name="cut_from" class="form-control" placeholder="00:00:00">
               </div>

               <div class="form-group">
                   <label>Duration</label>
                   <input type="text" name="duration" class="form-control" placeholder="00:00:00">
              </div>

              <input type="submit" name="submit" class="btn btn-primary" value="Split">

	    </form>
	</div>
    </div>
</div>

Step 3 – Split video in multiple parts

The next step is to split the file based on user input. Paste the following code on top of your file including the PHP starting and closing tags:

<?php

if (isset($_POST["submit"]))
{
    $file_name = $_FILES["video"]["tmp_name"];
    $cut_from = $_POST["cut_from"];
    $duration = $_POST["duration"];

    $command = "/usr/local/bin/ffmpeg -i " . $file_name . " -vcodec copy -ss " . $cut_from . " -t " . $duration . " output.mp4";
    system($command);
}

?>

Step 4 – Preview of selected video

At this point your script is working perfectly. One more thing you can add is to display the preview of video before performing the splitting action. So that the user can know which video he is about to split. To do this first you have to create a video tag just below input type file, so the preview of video will be displayed beneath the input field.

<video width="500" height="320" id="video" controls></video>

This will set the width and height of video. Make sure to give the unique “id” attribute and also the “controls” attribute. The id attribute will help in javascript to get the video tag and display the preview of selected video and controls attribute will display the controls in html5 video player.

Now you need to attach a javascript listener that will be called when the user selects the file. Go ahead and add an attribute on input type file tag.

onchange="onFileSelected(this);"

This will call the javascript function called “onFileSelected” and send the input type file as a parameter. Now we are going to create a function.

At the bottom of file paste the following code along with script tag at the start and end:

<script>

    function onFileSelected(self) {
        var file = self.files[0];
        var reader = new FileReader();

        reader.onload = function (event) {
            var src = event.target.result;
            var video = document.getElementById("video");
            video.setAttribute("src", src);
        };

        reader.readAsDataURL(file);
    }

</script>

Now that you have learned how to split the video file in multiple parts. You can also learn how to merge multiple videos into one. Follow this tutorial to do that.

[wpdm_package id=’150′]