Dailymotion API: Uploading, Modifying, and Viewing

Image Source

Dailymotion is a French video-sharing platform owned by Vivendi. In the US it is supported by partners like Vice Media, Bloomberg, and Hearst Digital Media. It is available in 183 languages ​​with 43 localized versions. It is currently estimated to have 300 million users, which is substantial but far below that of its larger competitor, YouTube.

The Dailymotion API is a set of developer tools that you can use to manage and interact with Dailymotion content. Through the three-component APIs, you can access most, if not all, aspects of Dailymotion. Public data is freely available while private data is restricted according to the permissions granted to your authentication status and permissions.

  • Data API: This API enables you to access, modify, and publish data. It enables you to work with a variety of Dailymotion objects, including videos, playlists, and users.

  • Player API: This API enables you to control a player that can be displayed or embedded in websites or applications. Embedding is compatible with most mobile devices as well as HTML-5 compliant smart devices.

  • Reporting API: This API enables you to build customizable reports that include aggregated performance measurements. It includes a flexible interface and supports multiple data formats. This API is only available to verified Dailymotion partners.

What Can You Do With Dailymotion’s API?

While there are many actions you can perform via the API, below are some of the most common and helpful capabilities.

Embed a Video on a Website

You can embed Dailymotion videos with their native player via an <iframe> element. To achieve this, you can use one of the following methods:

  • Via the embed_html field of the data API’s video object

  • Manually copy code from the export tab of the video page on Dailymotion

  • By using the oEmbed protocol

Whichever method you choose, the code for embed should look roughly as follows:

<iframe frameborder="0" width="480"

height="270"src="https://www.Dailymotion.com/embed/video/VIDEO_ID?PARAMS"allowfullscreen allow="autoplay"></iframe>

Customize a Video Player

Once a player is included in your site or app, you can customize the settings to match your overall theme and needs. This includes player color, behavior, controls, and logos. One way to set this customization is with params, query-string parameters. You can learn more about these parameters here.

To modify a player, you can define your params properties via the DM.player() method. This method is included in the JavaScript SDK. Below you can see an example of a customized player:

<script src="https://api.dmcdn.net/all.js"></script>

<div id="player"></div>

<script>var player = DM.player(document.getElementById("player"), {video: "VIDEO_ID", width: "100%", height: "100%", params: {autoplay: true, mute: true}});</script>

Add Subtitles to a Video

Dailymotion’s API includes methods that enable you to add or modify subtitles associated with your videos. This is done using subtitle objects that contain subtitle or closed caption information.

To create a new subtitle object, you can do so with a POST request through the video graph object. Below you can see an example request uploading a subtitle file:

curl -X POST -H "Authorization: Bearer ${ACCESS_TOKEN}"   

     -d 'language=en'  

     -d 'url=https://your_site.com/subtitle_file.srt'    

     -d 'format=SRT'       

     https://api.dailymotion.com/video/<VIDEO_ID>/subtitles

Protect a Video With a Password

If you have private videos or want to restrict video access to specific users, you can add password protection. You can do this by defining an access token and setting a password. Once a password is added, the visibility of the video changes to “password protected”.

You can see an example of this code below:

curl -X POST -H "Authorization: Bearer ${ACCESS_TOKEN}"  

     -d 'password=MyPassword'  

     https://api.dailymotion.com/video/myvideo

How to Upload and Publish Videos With Dailymotion API

The most basic and perhaps most important functionality of the Dailymotion API is the ability to upload and publish videos. Below you can see a brief walkthrough explaining the steps needed, including sample code. This is summarized from the Dailymotion documentation which you can see here.

1. Authenticate the User

Before you can get started with uploading, you need to authenticate users to ensure API access is secure. Determine which account you want the video to be hosted on. In this account, you then need to authenticate your user and request the manage_videos scope.

2. Get an Upload URL

Next, you can make a GET request, including your access token, to https://api.dailymotion.com/file/upload. Once this request is sent, you should receive the upload_url and a progress_url.

curl -H "Authorization: Bearer ${ACCESS_TOKEN}" 

     "https://api.dailymotion.com/file/upload"

When performing this step, you also have the option to enable end-users to post videos. To do this, you need to configure a callback_url parameter to call your service after the upload is complete.

3. Upload the Video

With your upload URL, you can now upload the video of your choice with a POST request. You should do this with the multipart/form-data content type to ensure the video is uploaded successfully.

curl -X POST  

     -F 'file=@/path/to/your/video.mp4'  

     '<UPLOAD_URL>'

After your request is made you should either receive a JSON object or your callback URL with additional query string parameters. From either, make note of the URL field.

Here is how to upload the file using the PHP API:

$url = $api->uploadFile('/path/to/your/video.mp4');

$api->post(
    '/videos',
    array(
        'url'       => $url,
        'title'     => 'Dailymotion PHP SDK upload test',
        'tags'      => 'dailymotion,api,sdk,test',
        'channel'   => 'videogames',
        'published' => true,
        'is_created_for_kids'   => false,
    )
);

4. Create the Video

Using the noted URL field, you now need to make another POST request to https://api.dailymotion.com/me/videos. This call creates the video and should return an ID number for your new video in the response.id field.

curl -X POST -H "Authorization: Bearer ${ACCESS_TOKEN}" 

           "https://api.dailymotion.com/me/videos"

5. Publish the Video

Assuming you’re ready to publish the video, you need to perform one more POST request to https://api.dailymotion.com/video/<VIDEO_ID>. In this request, you should include any fields that you want to modify before posting. You must also include channel, title, and tags fields as well as a published field that is set to true.

curl -X POST -H "Authorization: Bearer ${ACCESS_TOKEN}" 

     -d 'published=true' 

     -d 'title=<TITLE>' 

     -d 'channel=<CHANNEL>' 

     -d 'tags=<TAGS>' 

     https://api.dailymotion.com/video/<VIDEO_ID>

.

Leave a Comment