Exemple #1
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string kalturaUGCFilePath = ConfigurationManager.AppSettings["KalturaUGCFilePath"].ToString();

            try
            {
                if (uploader.HasFile)
                {
                    lblmsg.Text = "Upload media file";
                    System.Threading.Thread.Sleep(5000);
                    //Uncomment this line to Save the uploaded file
                    uploader.SaveAs(kalturaUGCFilePath + uploader.FileName);
                    Label1.Text = "Received " + uploader.FileName + " Content Type " + uploader.PostedFile.ContentType + " Length " + uploader.PostedFile.ContentLength;

                    lblmsg.Text = "Ready to upload file to kaltura media space";
                    //Create Session
                    int    partnerId = Convert.ToInt32(ConfigurationManager.AppSettings["KalturaPartnerID"].ToString());
                    string secret    = ConfigurationManager.AppSettings["KalturaAdminSecret"].ToString();
                    string userId    = ConfigurationManager.AppSettings["KalturaUserID"].ToString();
                    int    expiry    = Convert.ToInt32(ConfigurationManager.AppSettings["KalturaSessionExpiry"].ToString());

                    KalturaConfiguration config = new KalturaConfiguration(partnerId);

                    config.ServiceUrl = ConfigurationManager.AppSettings["KalturaServiceUrl"].ToString();//"https://www.kaltura.com/";// SERVICE_URL;
                    KalturaClient client = new KalturaClient(config);
                    client.KS = client.GenerateSession(secret, userId, KalturaSessionType.ADMIN, partnerId, expiry, "");

                    //Add entry
                    KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
                    mediaEntry.Name          = txtFriendlyName.Text; //"Media Entry Using C#";
                    mediaEntry.Description   = edDescription.Text;
                    mediaEntry.MediaType     = KalturaMediaType.VIDEO;
                    mediaEntry.Categories    = ConfigurationManager.AppSettings["KalturaUGCCategories"].ToString();
                    mediaEntry.CategoriesIds = ConfigurationManager.AppSettings["KalturaUGCCategoriesIds"].ToString();
                    //mediaEntry.ModerationStatus = KalturaEntryModerationStatus.PENDING_MODERATION;
                    mediaEntry = client.MediaService.Add(mediaEntry);

                    lblmsg.Text = "Added entry to kaltura media space";
                    //Upload token
                    FileStream         fileStream  = new FileStream(kalturaUGCFilePath + uploader.FileName, FileMode.Open, FileAccess.Read);
                    KalturaUploadToken uploadToken = client.UploadTokenService.Add();
                    client.UploadTokenService.Upload(uploadToken.Id, fileStream);
                    lblmsg.Text = "Update the upload token for the entry to kaltura media space";
                    //Attach Media Entry
                    KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
                    mediaResource.Token = uploadToken.Id;
                    mediaEntry          = client.MediaService.AddContent(mediaEntry.Id, mediaResource);

                    lblmsg.Text = "Attached media entry to kaltura media space";
                }
                else
                {
                    Label1.Text = "No uploaded file";
                }
            }
            catch (Exception ex)
            {
                lblmsg.Text = "There is some error while uploading media entry to kaltura media space.";
            }
        }
        /// <summary>
        /// Shows how to start session, create a mix, add media, and append it to a mix timeline using multi request
        /// </summary>
        private static void AdvancedMultiRequestExample()
        {
            KalturaClient client = new KalturaClient(GetConfig());

            client.StartMultiRequest();

            // Request 1
            client.SessionService.Start(ADMIN_SECRET, "", KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");
            client.KS = "{1:result}"; // for the current multi request, the result of the first call will be used as the ks for next calls

            FileStream fileStream = new FileStream("DemoVideo.flv", FileMode.Open, FileAccess.Read);

            // Request 2
            KalturaUploadToken uploadToken = client.UploadTokenService.Add();

            // Request 3
            uploadToken = client.UploadTokenService.Upload("{2:result}", fileStream);

            // Request 4
            KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
            mediaEntry.Name = "Media Entry Using C#.Net Client To Test Flavor Replace";
            mediaEntry.MediaType = KalturaMediaType.VIDEO;
            mediaEntry = client.MediaService.Add(mediaEntry);

            // Request 5
            KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
            mediaResource.Token = "{2:result:id}";
            mediaEntry = client.MediaService.AddContent("{4:result}", mediaResource);

            // map paramters from responses to requests according to response calling order and names to request calling order and C# method parameter name
            client.MapMultiRequestParam(2, ":id", 3, "uploadTokenId");
            client.MapMultiRequestParam(4, ":id", 5, "entryId");

            KalturaMultiResponse response = client.DoMultiRequest();

            foreach (object obj in response)
            {
                if (obj.GetType() == typeof(KalturaAPIException))
                {
                    Console.WriteLine("Error occurred: " + ((KalturaAPIException)obj).Message);
                }
            }

            // when accessing the response object we will use an index and not the response number (response number - 1)
            if (response[4].GetType() == typeof(KalturaMediaEntry))
            {
                KalturaMediaEntry newMediaEntry = (KalturaMediaEntry)response[4];
                Console.WriteLine("Multirequest newly added entry id: " + newMediaEntry.Id);
            }
        }
        /// <summary>
        /// Shows how to start session, create a mix, add media, and append it to a mix timeline using multi request
        /// </summary>
        private static void AdvancedMultiRequestExample()
        {
            FileStream fileStream = new FileStream("DemoVideo.flv", FileMode.Open, FileAccess.Read);

            KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
            mediaEntry.Name = "Media Entry Using C#.Net Client To Test Flavor Replace";
            mediaEntry.MediaType = KalturaMediaType.VIDEO;

            KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
            mediaResource.Token = "{1:result:id}";

            KalturaClient client = new KalturaClient(GetConfig());

            client.KS = client.GenerateSession(ADMIN_SECRET, "", KalturaSessionType.ADMIN, PARTNER_ID);
            client.StartMultiRequest();
            client.UploadTokenService.Add();
            client.MediaService.Add(mediaEntry);
            client.UploadTokenService.Upload("{1:result:id}", fileStream);
            client.MediaService.AddContent("{2:result:id}", mediaResource);

            KalturaMultiResponse response = client.DoMultiRequest();

            foreach (object obj in response)
            {
                if (obj is KalturaAPIException)
                {
                    Console.WriteLine("Error occurred: " + ((KalturaAPIException)obj).Message);
                }
            }

            // when accessing the response object we will use an index and not the response number (response number - 1)
            if (response[3] is KalturaMediaEntry)
            {
                KalturaMediaEntry newMediaEntry = (KalturaMediaEntry)response[3];
                Console.WriteLine("Multirequest newly added entry id: " + newMediaEntry.Id + ", status: " + newMediaEntry.Status);
            }
        }
        // This will guide you through uploading a video, getting a specific transcoding flavor, replacing a flavor, and uploading a caption file.
        static void SampleReplaceVideoFlavorAndAddCaption()
        {
            // Upload a file
            Console.WriteLine("1. Upload a video file");
            FileStream fileStream = new FileStream("DemoVideo.flv", FileMode.Open, FileAccess.Read);
            KalturaClient client = new KalturaClient(GetConfig());
            string ks = client.GenerateSession(ADMIN_SECRET, USER_ID, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "");
            client.KS = ks;
            KalturaUploadToken uploadToken = client.UploadTokenService.Add();
            client.UploadTokenService.Upload(uploadToken.Id, fileStream);
            KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
            mediaResource.Token = uploadToken.Id;
            KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
            mediaEntry.Name = "Media Entry Using C#.Net Client To Test Flavor Replace";
            mediaEntry.MediaType = KalturaMediaType.VIDEO;
            mediaEntry = client.MediaService.Add(mediaEntry);
            mediaEntry = client.MediaService.AddContent(mediaEntry.Id, mediaResource);

            //verify that the account we're testing has the iPad flavor enabled
            int? flavorId = CheckIfFlavorExist("iPad");
            if (flavorId == null)
            {
                Console.WriteLine("!! Default conversion profile does NOT include the new iPad flavor");
                Console.WriteLine("!! Skipping the iPad flavor replace test, make sure account has newiPad flavor enabled.");
            }
            else
            {
                int iPadFlavorId = (int)flavorId; //C# failsafe from nullable int - we cast it to int
                Console.WriteLine("** Default conversion profile includes the new iPad flavor, id is: " + iPadFlavorId);

                //Detect the conversion readiness status of the iPad flavor and download the file when ready -
                Boolean statusB = false;
                KalturaFlavorAsset iPadFlavor = null;
                while (statusB == false)
                {
                    Console.WriteLine("2. Waiting for the iPad flavor to be available...");
                    System.Threading.Thread.Sleep(5000);
                    KalturaFlavorAssetFilter flavorAssetsFilter = new KalturaFlavorAssetFilter();
                    flavorAssetsFilter.EntryIdEqual = mediaEntry.Id;
                    KalturaFlavorAssetListResponse flavorAssets = client.FlavorAssetService.List(flavorAssetsFilter);
                    foreach (KalturaFlavorAsset flavor in flavorAssets.Objects)
                    {
                        if (flavor.FlavorParamsId == iPadFlavorId)
                        {
                            iPadFlavor = flavor;
                            statusB = flavor.Status == KalturaFlavorAssetStatus.READY;
                            if (flavor.Status == KalturaFlavorAssetStatus.NOT_APPLICABLE)
                            {
                                //in case the Kaltura Transcoding Decision Layer decided not to convert to this flavor, let's force it.
                                client.FlavorAssetService.Convert(mediaEntry.Id, iPadFlavor.FlavorParamsId);
                            }
                            Console.WriteLine("3. iPad flavor (" + iPadFlavor.FlavorParamsId + "). It's " + (statusB ? "Ready to ROCK!" : "being converted. Waiting..."));
                        }
                    }
                }

                //this is the download URL for the actual Video file of the iPad flavor
                string iPadFlavorUrl = client.FlavorAssetService.GetDownloadUrl(iPadFlavor.Id);
                Console.WriteLine("4. iPad Flavor URL is: " + iPadFlavorUrl);

                //Alternatively, download URL for a given flavor id can also be retrived by creating the playManifest URL -
                string playManifestURL = "http://www.kaltura.com/p/{partnerId}/sp/0/playManifest/entryId/{entryId}/format/url/flavorParamId/{flavorParamId}/ks/{ks}/{fileName}.mp4";
                playManifestURL = playManifestURL.Replace("{partnerId}", PARTNER_ID.ToString());
                playManifestURL = playManifestURL.Replace("{entryId}", mediaEntry.Id);
                playManifestURL = playManifestURL.Replace("{flavorParamId}", iPadFlavor.FlavorParamsId.ToString());
                playManifestURL = playManifestURL.Replace("{ks}", client.KS);
                playManifestURL = playManifestURL.Replace("{fileName}", mediaEntry.Name);
                Console.WriteLine("4. iPad Flavor playManifest URL is: " + playManifestURL);

                //now let's replace the flavor with our video file (e.g. after processing the file outside of Kaltura)
                FileStream fileStreamiPad = new FileStream("DemoVideoiPad.mp4", FileMode.Open, FileAccess.Read);
                uploadToken = client.UploadTokenService.Add();
                client.UploadTokenService.Upload(uploadToken.Id, fileStreamiPad);
                mediaResource = new KalturaUploadedFileTokenResource();
                mediaResource.Token = uploadToken.Id;
                KalturaFlavorAsset newiPadFlavor = client.FlavorAssetService.SetContent(iPadFlavor.Id, mediaResource);
                Console.WriteLine("5. iPad Flavor was replaced! id: " + newiPadFlavor.Id);
            }

            //now let's upload a new caption file to this entry
            FileStream fileStreamCaption = new FileStream("DemoCaptions.srt", FileMode.Open, FileAccess.Read);
            uploadToken = client.UploadTokenService.Add();
            client.UploadTokenService.Upload(uploadToken.Id, fileStreamCaption);
            KalturaCaptionAsset captionAsset = new KalturaCaptionAsset();
            captionAsset.Label = "Test C# Uploaded Caption";
            captionAsset.Language = KalturaLanguage.EN;
            captionAsset.Format = KalturaCaptionType.SRT;
            captionAsset.FileExt = "srt";
            captionAsset = client.CaptionAssetService.Add(mediaEntry.Id, captionAsset);
            Console.WriteLine("6. Added a new caption asset. Id: " + captionAsset.Id);
            KalturaUploadedFileTokenResource captionResource = new KalturaUploadedFileTokenResource();
            captionResource.Token = uploadToken.Id;
            captionAsset = client.CaptionAssetService.SetContent(captionAsset.Id, captionResource);
            Console.WriteLine("7. Uploaded a new caption file and attached to caption asset id: " + captionAsset.Id);
            string captionUrl = client.CaptionAssetService.GetUrl(captionAsset.Id);
            Console.WriteLine("7. Newly created Caption Asset URL is: " + captionUrl);
        }