C# Heywatch video encoding API Convert your video to different formats

The C# code sample below demonstrates how to convert your videos to different formats using Heywatch video encoding API.

What is Heywatch?

Today, Heywatch's video encoding engine is powered by the latest and most powerful cloud video encoding software. It’s easy to convert your videos to different formats at different encoding settings at very low cost. They support the most popular video formats: HTML5, iOS, Android, WebM, MP4, H.264, and FLV. And soon VP9, x265, H265 and HEVC etc. Video encoding involves huge cost for different audio/video codecs, but Heywatch is the place where you can do that at affordable cost.

Why Heywatch API ?

Using Heywatch API, you can easily integrate the video conversion and encoding features in your applications easily and at a very low cost. HeyWatch's video encoding API allows to easily integrate their video encoding software in your application using different platforms and technologies. It involves 3 easy steps:
1.  Upload
2.  Encoding
3.   Delivery

Using your own C# coding to upload and encode your videos on Heywatch

This involves the following steps:
1. You need to create an account on Heywatch and get Heywatch credentials.
2. Upload your video on from your server or ftp location.
3. Create a job to encode your video
4. Get your encoding video

Step 1: Heywatch API Key:
You need to sign up and get your API key to start encoding videos from your applications. You can learn more at https://app.heywatch.com/settings. Also you can use the Heywatch Username and Password to make requests.
Step 2: Upload your video to Heywatch Account:
You need to use download post request to upload your videos to Heywatch accounts. After successful upload, downloadid is sent with downloadinfo sent at download ping_url you passed with the request.
Step 3: Create a job:
To encode your uploaded video in heywatch account, you need to send a job post request using the downloadid you get in Step 2. After the job is created, you get a jobid, which you need to save for future references.
Step 4: Get your encoded video:
You can get the encoded video from the videoinfo sent by heywatch on your ping_url send with the job request after its encoded. You can extract the info from the response by using the jobid, which was saved in Step 3.

Sample Code to create download and job request using C#

private string SendVideoToHeywatch()
{
        HttpWebRequest request;
        HttpWebResponse response;
        NetworkCredential cred;
        StringBuilder data =
new StringBuilder();
        Byte[] ByteData;
        
try
        {
             string title =
"Test video";
string requestId = 123;
//It can be any DB autofield or unique number you sent for your ref- This can be used to verify the video while ping url is pinged by Heywatch
             string fileStoragePath =
"C:/testvideo.wmv";
string pingurl =
"http://xxxxxxx.com/downloadstatus.aspx"; // ping back url of your website where heywatch sends the status for the video transfer
           cred =
new NetworkCredential(heywatchUsername, heywatchPassword);
             request = (HttpWebRequest)WebRequest.
Create("http://heywatch.com/download.xml");
             request.Method =
"POST";
             request.ContentType =
"application/x-www-form-urlencoded";
             request.Credentials = cred;
             request.PreAuthenticate =
true;
             request.KeepAlive =
true;

              data.Append(
"url=" + System.Web.HttpUtility.UrlEncode(fileStoragePath));
              data.Append(
"&title=" + title + "&ping_url="+ pingurl + "&VideoID=" + requestId.ToString());

             ByteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
             request.ContentLength = ByteData.Length;
             request.Timeout = 300000;
             Stream PostStream = request.GetRequestStream();
              PostStream.
Write(ByteData, 0, ByteData.Length);

             response = (HttpWebResponse)request.GetResponse();
             StreamReader reader =
new StreamReader(response.GetResponseStream());
             String result = reader.ReadToEnd();
             XmlDocument xmlResult =
new XmlDocument();
             xmlResult.LoadXml(result);
             XmlNode xmlID = xmlResult.SelectSingleNode(
"/download/id");
            
return xmlID.InnerText; // Return the Download Id returned from heywatch
             response.
Close();
             request =
null;
        }
        catch (Exception ex)
        {
            
//Handle exception accordingly
        }
}

When you get back the ping on downloadstatus.aspsx, you can extract the following VideoId (once we sent with the download request and heywatchvideoid
as video_id)

Request.Form[
"VideoID"],heywatchvideoid= Request.Form["video_id"]

Video_id
is the unique id created for the uploaded video in your heywatch watch account

private void CreateJOBOnHeywatch()
    {
        HttpWebRequest request;
        HttpWebResponse response;
        NetworkCredential cred;
        Byte[] ByteData;
        StringBuilder data =
new StringBuilder();
        
         
try
        {
            cred =
new NetworkCredential(heywatchUsername, heywatchPassword);
            request = (HttpWebRequest)WebRequest.
Create("http://heywatch.com/job.xml");
            request.Method =
"POST";
            request.ContentType =
"application/x-www-form-urlencoded";
            request.Credentials = cred;
            request.PreAuthenticate =
true;
            request.KeepAlive =
true;

           data.Append(
"format_id=" + formatId); //formatId is the format conversion settings you made at Heywatch       
            data.Append(
"&video_id=" + heywatchvideoid.ToString());
             
//watermark
            
if (addWaterMark)
             {
                 data.Append(
"&watermark_image_url=" + waterMarkUrl);
                 data.Append(
"&watermark_position=" + ConfigurationManager.AppSettings["WatermarkPosition"]);
            }
          
            data.Append(
"&keep_video_size=true");// Optional
          
             data.Append(
"&keep_resolution=true");
            data.Append(
"&ping_url=" + ConfigurationManager.AppSettings["StatusUrl"]);
            
            
if (S3Storage == true)
            {
                 data.Append(
"&output_url=s3://accesskey:secretkey@bucket/my/object/file.mp4");
            }
            
else//if ftp
            {
data.Append(
"&output_url=ftp://" + ftpUsername.Replace("@", "%40") + ":"
                  + ftpPassword.Replace(
"@", "%2540")
                  +
"@" + ftpOutputPath);

            }
            
            ByteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
            request.ContentLength = ByteData.Length;
            request.Timeout = 300000;
            Stream PostStream = request.GetRequestStream();
             PostStream.
Write(ByteData, 0, ByteData.Length);
            response = (HttpWebResponse)request.GetResponse();
            StreamReader reader =
new StreamReader(response.GetResponseStream());
            String result = reader.ReadToEnd();
            XmlDocument xmlResult =
new XmlDocument();
            xmlResult.LoadXml(result);
            XmlNode xmlID = xmlResult.SelectSingleNode(
"/job/id");
            heywatchVideoJobID =
Convert.ToInt32(xmlID.InnerText); // get the heywatch video job id
          
             response.
Close();
            request =
null;

            
        }
        catch {
//Handle Exceptions accordingly}
    }


By Sushil Kumar   Popularity  (2438 Views)