How to upload multiple videos from your server to your Ziggeo account?

Were you wondering how you could transfer all of your already recorded cool videos to your Ziggeo application? Did you think that it would take some time to do it? Well, if you use Ziggeo PHP SDK, no more need to wait, just download and set up your pages to run the code as in one of our demo and you are all set. This is the link to the demo: Upload multiple videos from command line demo. Now lets actually go through the code together and see what happens and how.

Prepping the code

First what we recommend is to set up / define the keys that we will use. This allows us to update the same in only one location and have it work everywhere else. To do that we use:
[code language="php"]
define('APP_TOKEN', 'OUR_APP_TOKEN');
define('PRIVATE_KEY', 'OUR_PRIVATE_KEY');
[/code]
Now we can use that anywhere in the code, calling it as APP_TOKEN or PRIVATE_KEY. OK, so this is all basic and clear, and you want to jump to the actual code that does the neat transfer. We hear you, so lets jump.

Accepting command line calls

Command line calls are great for several reasons: 1. You can set it up so that your crontab (cron job) can run this for you and upload some videos at a time. 2. You run only the php service, not the webserver and all the other services that run with them (so there is a bit less load on your server) To accept command line calls, we check for parameters passed over:
[code language="php"]
if(isset($argv, $argv[1], $argv[2])) {
    //we do, great
    if(function_exists($argv[1]))
    {
        if(!isset($argv[3])) {
            //Calling the right function with just the video folder
            $argv[1]($argv[2]);
        }
        elseif(!isset($argv[4])) {
            //calling the function with video folder and custom tags
            $argv[1]($argv[2], $argv[3]);
        }
        else {
            //calling the function with video folder, tags and files to ignore.
            $argv[1]($argv[2], $argv[3], $argv[4]);
        }
    }
}
[/code]
Now the first thing to know is how we actually call our script from command line. That will explain the code above, so in our case, it would be something like this:
serviceName /path/to/php/script.php functionToCall /path/to/videos/directory/ optionalTags optionalIgnoreList
Each space would mean that new argument (argv) is starting, and the serviceName is called as it is, while the rest are its arguments. Knowing this we get (for example and could be different on your side):
[code]
serviceName = php
/path/to/php/script.php = /var/www/html/ziggeo/bulk_uploads_cli.php
functionToCall = pushVideos
/path/to/videos/directory/ = /var/www/videos/
optionalTags = "tag1,tag2,tag3"
optionalIgnoreList = ".,..,"
[/code]
As such our call would actually look like:
php /var/www/html/ziggeo/bulk_uploads_cli.php pushVideos /var/www/videos/ "tag1,tag2,tag3" ".,..,"
*Of course, optional parameters do not need to be set. Now going back to the code above we can see our parameters:
[code]
$argv[0] - the file where the code is located (we simply ignore this parameter)
$argv[1] - the function name (pushVideos)
$argv[2] - directory where the videos are
$argv[3] - optional tags
$argv[4] - optional ignore list
[/code]
Now, our code with arguments is simply checking if we had mentioned tags. Since they are optional, and if they are not set, we just call the code with defaults:
[code language="php"]
    if(!isset($argv[3])) {
        //Calling the right function with just the video folder
        $argv[1]($argv[2]);
    }
[/code</pre>
*Since tags are optional, and if they are not set, it means that ignore list is not set neither, so check for one is enough as we know the other is not set neither.

Now, if this is not true (we do have tags set), we do a check for optional ignore list
<pre>[code language="php"]
    elseif(!isset($argv[4])) {
        //calling the function with video folder and custom tags
        $argv[1]($argv[2], $argv[3]);
    }
[/code]
If that is not set, we call the pushVideos with tags and without ignore list Alternatively, we call the function by passing all parameters to it:
[code language="php"]
    else {
        //calling the function with video folder, tags and files to ignore.
        $argv[1]($argv[2], $argv[3], $argv[4]);
    }
[/code]
* This parameter processing could be done differently, so feel free to change it up to match your own requirements. Now the next important step for us is the following call:
[code language="php"]
$files = @scandir($videosDir);
[/code]
It helps us scan the directory that we have passed to search for all the videos in it. Once we get the list back, we check it out and remove the elements from ignore list:
[code language="php"]
    if($files) {
        //we run it like this, which will remove . and .. and other files if you have specified any with full name
        $files = array_diff($files, $ignoreList);
    }
[/code]
And the reason why we do not just go and remove the same (without the check) is because sometimes we could be reported an error instead of the video file names. For example due to permission, or if there are no videos found, etc. This is picked up by the next code for us:
[code language="php"]
    if(!$files) {
        echo 'No videos found to execute the push';
        exit;
    }
[/code]
Now, we start the actual upload:
[code language="php"]
    foreach($files as $file) {
[/code]
This allows us to grab each item in the array and use one by one. You could use for() loop as well, however it is good to point out that array_diff call we used above will remove the items from array. As it does it will not repair the index, so if it was to remove first 2 and 4th item in the list (per our ignorelist parameter), for() loop would need to include a check if the same array index is still set or not - or you would have a few errors shown. Foreach however does not have the same issue, as it does not care about the specific index, rather grabs the next array index available for us.
[code language="php"]
    $tmp_arguments = $arguments;
    $tmp_arguments['file'] = $videosDir . '/' . $file;

    $ziggeo->videos()->create($tmp_arguments);
[/code]
At this point we are actually sending the files. First we set up the arguments to pass over (we need to pass the array) so that it includes the tags previously set and the path to video (this changes each time). Once we call create() that is it. The video has already started its journey to our servers and you are left to do is to wait for it to finish - or simply enjoy and check it out in a bit to see how it went. With such code base you could do many things - use some form of progress bar since we know the number of videos and could derive the percentage of the same. You could also check the video sizes (individual and sum) and have estimated time shown, and many other neat things. PS: Did you know that $ziggeo->videos()->create($tmp_arguments); works in other SDKs as well? Yup, the only difference would be the code around Ziggeo SDK - the one you make, so the same can be easily applied to other SDKs as well. Was this useful for you and did you already try it out? Let us know and share with others.
PREV NEXT