Create KS protected videos with free preview

by Zohar Babin

Creating protected videos is a request that comes up once in a while in talks and on the forums.
Whether you’d like to sell the videos or allow restricted access for members only, this short tutorial is going to set the ground for it.

The KS Protected Access Control is a way to protect the access to your videos by enforcing the need to pass a KS (Kaltura Session) for the request that serves the video.

It is an advanced method meant to provide the developer of the integrated system means to decide on his side who should have access (valid KS specified) to the videos and who will be denied (no KS provided).

Step 1 – Create an Access Control Profile
Follow these steps:
a. Go to Settings > Access Control > Add Profile.
b. Scroll the window down to “Advanced Security & Pay-per-view”.
c. Check the two boxs (“Secure viewing of this video using server side session” and “Free Preview”).
d. Set the time for the preview period.

Or.. since video is much better than words, watch this short video:

Step 2 – Write the PHP code
We’ll use the PHP API Client library, this of course for the sake of example, you can use any other language for that.
The important part is the definition of the client object, configuring it and creation of the Kaltura Session (KS):

The KS (Kaltura Session) is constructed of a number of elements and must be generated on the system side:

  1. The API secret (KMC>Settings>Integration Settings>User Secret).
  2. The system (your WordPress) user Id.
  3. The Kaltura Session Type (USER in this case, if you use a PHP client library: KalturaSessionType::USER).
  4. Your Kaltura Partner Id.
  5. The Session Expiry in milliseconds.
  6. A special privileges string – this is very important in this case as it is used to determine if the user has access to see the full video or not, it is specified using the string ‘sview:’ with the video entry id concatenated (e.g. ‘sview:1_92cyew21′).
// Your Kaltura credentials
define("PARTNER_ID", "--Set your partner id here--");
define("USER_SECRET", "--Set your API user secret here (KMC>Settings>Integration Settings>User Secret)--");
define("ENTRY_ID", "--Set the entry id of a video with relevant KS access control applied that belong to the above partner id--");
define("UICONF_ID", "--Set the uiconf id of the player you'd like to use. You can find this id in the players list on the Studio tab in KMC--");
// Whatever user id you want to set (This is according to your system deployment):
$user = "SomeoneWeKnow";

//This is the session start function signature: start($secret, $userId = "", $type = 0, $partnerId = -1, $expiry = 86400, $privileges = "")
$session = $client->session->start(USER_SECRET, $user, KalturaSessionType::USER, PARTNER_ID, 86400, 'sview:'.ENTRY_ID);

Note “sview:ENTRY_ID”
To create a KS (Kaltura Session) that will provide access to the video, pass “sview:ENTRY_ID” in the permissions variable when creating a new KS (using the session create function), replacing ENTRY_ID with the id of the video entry to allow access to.
This generated KS should then be rendered to the page if the user has permissions to access the full video.
If the user doesn’t have permissions to access the full video, flashvars shouldn’t render a KS and Kaltura will only stream the preview part of the video as defined in the access control profile.

Then, on the flashvars of the embedded KDP, we’ll provide the created ks as follow:

kWidget.embed( 'playerTarget', {
	'wid' : '_243342',
	'uiconf_id' : '8145862',
	'entry_id' : '1_20x0ca3l',
	'flashvars':{
		'ks': < ?php echo $session; ?>
	}
});

All done. :-)

Download the sample PHP app here.

If that helped you, or you find a good way to improve it – let us know in the comments / twitter..

8 Comments to “Create KS protected videos with free preview”

  1. RE: http://india551.server4you.de/dev/wordpress/blog/2011/01/27/kaltura-support/
    Seem the article needed a bit more clarification, so I’m adding as a comment.

    The KS Protected Access Control is a way to protect the access to your videos by enforcing the need to pass a KS (Kaltura Session) for the request that serves the video.

    It is an advanced method meant to provide the developer of the integrated system means to decide on his side who should have access (valid KS specified) to the videos and who will be denied (no KS provided).

    It might be confusing, this functionality is based on the assumption that the target system is holding the rules whether a user X should have access to the video.

    The KS (Kaltura Session) is constructed of a number of elements and must be generated on the system side (your WordPress site in this case):
    The API secret (KMC>Settings>Integration Settings>User Secret).
    The system (your WordPress) user Id.
    The Kaltura Session Type (USER in this case, if you use a PHP client library: KalturaSessionType::USER).
    Your Kaltura Partner Id.
    The Session Expiry in milliseconds.
    A special privileges string – this is very important in this case as it is used to determine if the user has access to see the full video or not, it is specified using the string ‘sview:’ with the video entry id concatenated (e.g. ‘sview:1_92cyew21′).

    The KsProtectedPlaybackWithPreview.php file is only a sample of how you should use this functionality to integrate in your own site. In your case, you’d need to implement this as a function in the WordPress Kaltura plugin you use, then according to your own rules, if the user should have access to the full video, you should generate the KS and pass to the embedded player.

  2. Hi Zohar,

    Great post. One question: this will work for flash-based Kaltura player but how does preview mode work with your HTML5 player. We tried your example and it works perfectly for flash player but when we include the http://html5.kaltura.org/js and try it with an ipad, we end up seeing entire video instead of just the preview.

    Any thoughts?

  3. Hi Kabir,

    It works the same with the html5 video lib.
    Did you set up the Access Control correctly?

  4. Hi Zohar,

    I think so. I sent you an email with a client reference (partner id) where we need this working with HTML5. I sent you link to the code we are using. Frankly, all we did is followed the example you have here and then added the JS lib which automatically replaces the flash object by targeting the player id and should have worked.

    Did you get my email with code link?
    Thanks!

    Kabir

  5. At the end of the free preview, a dialog opens with the title “Free Preview is Complete” and a message that says “Access to the rest of the content is restricted”. How can I stop that dialog from displaying?

    I’ve got a JavaScript listener set up to handle the freePreviewEnd notification – and I’m guessing I can send the showUiElement notification with the name of that dialog to hide it – but I don’t know the name of the dialog. And I’m not sure that’s the best user-experience. Ideally, the dialog wouldn’t show up in the first place.

  6. Works great for me, but it’s not exactly what I’m looking for.
    I would like to END a movie after 15 seconds programmaticaly for those not logged in, without altering the Access Controll on the Entry via KMC. Is it possible, and if, how?
    I tried using mediaProxy.mediaPlayTo in the KMC but that only pauses the movie, making the rest of the movie avaliable once you hit Play again. I would like it to END.

    • Hi there, what’s the reason you wish to avoid access control?
      without setting an access control, Kaltura will not know that access to the video should be restricted.
      So you will be able to pause the video without ACL, but if you wish to stop streaming and restrict the access, ACL is the way to go.

  7. Hello, this weekend is pleasant designed for me, as this moment i am reading this fantastic educational
    article here at my residence.

Leave a Reply