Windows Azure Media Services: Java SDK のサンプルコード (VOD編)

Javaのサンプルコードはあまり多く外部に出ていませんので、ここで一通りの流れのものを。

  • JRE 1.7 で動作確認済み
  • Windows Azure SDK for Javaの設定を。
  • Ingest -> Encode -> Origin までの一連の流れです。Origin ServerはDynamic Packaging用です。
  • Playerに渡す文字列はHLS用にしています。Smooth Streaming用は、/manifestまで。いいですかね(笑)。ご存じですよね(笑)。

Enjoy!

—————————————————————————————–

iimport java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.InputStream;

import java.util.EnumSet;

import com.microsoft.windowsazure.services.blob.models.CreateBlobOptions;

import com.microsoft.windowsazure.services.core.Configuration;

import com.microsoft.windowsazure.services.core.ServiceException;

import com.microsoft.windowsazure.services.media.MediaConfiguration;

import com.microsoft.windowsazure.services.media.MediaContract;

import com.microsoft.windowsazure.services.media.MediaService;

import com.microsoft.windowsazure.services.media.WritableBlobContainerContract;

import com.microsoft.windowsazure.services.media.models.AccessPolicy;

import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;

import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;

import com.microsoft.windowsazure.services.media.models.Asset;

import com.microsoft.windowsazure.services.media.models.AssetFile;

import com.microsoft.windowsazure.services.media.models.AssetFileInfo;

import com.microsoft.windowsazure.services.media.models.AssetInfo;

import com.microsoft.windowsazure.services.media.models.AssetOption;

import com.microsoft.windowsazure.services.media.models.ErrorDetail;

import com.microsoft.windowsazure.services.media.models.Job;

import com.microsoft.windowsazure.services.media.models.JobInfo;

import com.microsoft.windowsazure.services.media.models.JobState;

import com.microsoft.windowsazure.services.media.models.ListResult;

import com.microsoft.windowsazure.services.media.models.Locator;

import com.microsoft.windowsazure.services.media.models.LocatorInfo;

import com.microsoft.windowsazure.services.media.models.LocatorType;

import com.microsoft.windowsazure.services.media.models.MediaProcessor;

import com.microsoft.windowsazure.services.media.models.MediaProcessorInfo;

import com.microsoft.windowsazure.services.media.models.Task;

import com.microsoft.windowsazure.services.media.models.TaskInfo;

public class WAMSJava {

    /**

     * @param args

     */

    public static void main(String[] args) {

     String mediaServiceUri    = “https://media.windows.net/API/”;

     String oAuthUri    = “https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13”;

     String clientId        = “<Windows Azure Media Services account name>”; // Use your media service account name.

     String clientSecret    = “<Windows Azure Media Services account key>”; // Use your media service access key.

     String scope        = “urn:WindowsAzureMediaServices”;

     try {

         // Windows Azure Media Services と接続

         Configuration configuration = MediaConfiguration.configureWithOAuthAuthentication(mediaServiceUri, oAuthUri, clientId, clientSecret, scope);

         MediaContract mediaService = MediaService.create(configuration);

         // 1) Ingest

         System.out.println(“1) Start Ingest…”);

         // The local file that will be uploaded to your Media Services account.

         File targetFile = new File(“C:/Videos/xbox/Halo4_AnimatedKey_WebHi.wmv”);

         InputStream input = new FileInputStream(targetFile);

         // Create an asset

         AssetInfo ingestAsset = mediaService.create(Asset.create()

                  .setName(targetFile.getName())

                  .setOptions(AssetOption.StorageEncrypted));

         System.out.println(” Created asset with id: ” + ingestAsset.getId());

         // Create an access policy that provides Write access for 60 minutes.

         AccessPolicyInfo accessPolicy = mediaService.create(

                  AccessPolicy.create(“uploadAccessPolicy”, 60.0,

                          EnumSet.of(AccessPolicyPermission.WRITE)));

         // Create a locator using the access policy and asset.

         // This will provide the location information needed to add files to the asset.

         LocatorInfo locator = mediaService.create(

                  Locator.create(accessPolicy.getId(),

                          ingestAsset.getId(),

                          LocatorType.SAS));

         // Create the blob writer using the locator.

         WritableBlobContainerContract uploader = mediaService.createBlobWriter(locator);

         CreateBlobOptions uploadOption = new CreateBlobOptions();

         uploadOption.setTimeout(60);

         uploader.createBlockBlob(targetFile.getName(), null);

         // Inform Media Services about the uploaded files.

         mediaService.action(AssetFile.createFileInfos( ingestAsset.getId()));

         System.out.println(” Ingest Finished.”);

         // 2) Encode

         // Use the Windows Azure Media Encoder, by specifying it by name.

         System.out.println(“2) Encoding Started…”);

         MediaProcessorInfo mediaProcessor = mediaService.list(MediaProcessor.list().set(“$filter”, “Name eq ‘Windows Azure Media Encoder'”)).get(0);

         // Create a task with the specified media processor, in this case to transform the original asset to the H.264 HD 720p VBR preset.

         // Information on the various configurations can be found at

         // http://msdn.microsoft.com/en-us/library/microsoft.expression.encoder.presets_members%28v=Expression.30%29.aspx.

         // This example uses only one task, but others could be added.

         Task.CreateBatchOperation task = Task.create(

         mediaProcessor.getId(),

         “<taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetName='” + ingestAsset.getName() + “‘>JobOutputAsset(0)</outputAsset></taskBody>”)

         .setConfiguration(“H264 Adaptive Bitrate MP4 Set SD 4×3”)

         .setName(“java SDK Task”);

         // Create a job creator that specifies the asset, priority and task for the job.

         Job.Creator jobCreator = Job.create()

         .setName(“java SDK job”)

         .addInputMediaAsset(ingestAsset.getId())

         .setPriority(2)

         .addTaskCreator(task);

         // Create the job within your Media Services account.

         // Creating the job automatically schedules and runs it.

         String jobId = mediaService.create(jobCreator).getId();

         System.out.println(” Execute Media processor.”);

         JobState jobResult = CheckJobStatus(mediaService, jobId);

         System.out.println(” Finished Encoding”);

         if (jobResult == JobState.Error ) {

              return;

         }

         ListResult<AssetInfo> encodedAssets = mediaService.list(Asset.list(

                          mediaService.get(Job.get(jobId)).getOutputAssetsLink()

                              )

                          );

         AssetInfo encodedAsset = null;

         if (encodedAssets.size()>0){

              encodedAsset = encodedAssets.get(0);

         }

         // 3) Delivery

         System.out.println(“3) Start Delivery…”);

         AccessPolicyInfo downloadAccessPolicy = mediaService.create(

                 AccessPolicy.create(“Download”, 60.0, EnumSet.of(AccessPolicyPermission.READ)));

         LocatorInfo originlocator = mediaService.create(

                 Locator.create(downloadAccessPolicy.getId(), encodedAsset.getId(), LocatorType.OnDemandOrigin));

         AssetFileInfo manifest = mediaService.list(AssetFile.list(encodedAsset.getAssetFilesLink()).set(“$filter”, “endswith(Name,’.ism’)”)).get(0);

         String basePlayerURL = originlocator.getPath() + manifest.getName();

         System.out.println(“PlayerURL: ” + basePlayerURL + “/manifest(format=m3u8-aapl)”);

         File logFile = new File(System.getenv(“USERPROFILE”) + “/Desktop/WAMS_java_PlayerURL.txt”);

         BufferedWriter log = new BufferedWriter(new FileWriter(logFile));

         log.write(basePlayerURL + “/manifest(format=m3u8-aapl)”);

         log.flush();

         log.close();

         System.out.println(“All task completed”);

     } catch (ServiceException se) {

     System.out.println(“ServiceException encountered.”);

     System.out.println(se.getMessage());

     } catch (Exception e) {

     System.out.println(“Exception encountered.”);

     System.out.println(e.getMessage());

     }

     System.out.println(“All process completed!”);

}

    // Helper function to check to on the status of the job.

    private static JobState CheckJobStatus(MediaContract mediaService, String jobId)

            throws InterruptedException, ServiceException

    {

        Boolean jobCompleted = false;

        JobState jobState = null;

     while (!jobCompleted)

     {

         JobInfo currentJob = mediaService.get(Job.get(jobId));

         jobState = currentJob.getState();

         System.out.println(” Job state is ” + jobState);

         switch(jobState){

             case Finished:

             case Canceled:

                 jobCompleted = true;

                 System.out.println(” Job finished.”);

                 break;

             case Error:

                 jobCompleted = true;

              ListResult<TaskInfo> errTasks = mediaService.list(Task.list(currentJob.getTasksLink()));

              for (TaskInfo task : errTasks) {

              System.out.println(” Error::” + task.getName());

              for (ErrorDetail detail : task.getErrorDetails()) {

              System.out.println(detail.getMessage());

              }

              }

                 break;

             case Processing:

              ListResult<TaskInfo> currentTasks = mediaService.list(Task.list(currentJob.getTasksLink()));

              for (TaskInfo task : currentTasks) {

              System.out.println(” ” + task.getName() + “:” + task.getProgress() + “%”);

              }

             case Scheduled:

             case Queued:

             case Canceling:

         }

     Thread.sleep(10000); // Sleep for 10 seconds, or use another interval.

     }

     return jobState;

    }

}

広告

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中