/// <summary>
        /// Adds a Captioning to an existing video
        /// http://docs.brightcove.com/en/media/reference.html#Video_Write
        /// The captions can either be uploaded to BC or be referred to by an external url
        /// BrightCove supports the DFXP and SMPTE-TT formats
        /// http://support.brightcove.com/en/docs/using-captions-smart-player-api
        /// </summary>
        /// <param name="caption_source">Metadata about the captions</param>
        /// <param name="options">Parameters (options) including the ID of the video that MUST be set</param>
        /// <param name="captions">a buffer containing DFXP or SMPTE-TT caption data</param>
        /// <returns>the captionSources object</returns>
        public RPCResponse <BCCaptionSources> AddCaptioning(BCCaptionSource caption_source, BCAddCaptioningOptions options, byte[] captions)
        {
            Dictionary <string, object> postParams = new Dictionary <string, object>();

            Builder builder = new Builder()
                              .AppendObject("caption_source", caption_source.ToJSON(JSONType.Create))
                              .Append(",").AppendField("token", Account.WriteToken.Value);

            if (!string.IsNullOrEmpty(options.filename))
            {
                builder.Append(",").AppendField("filename", options.filename);
            }

            if (options.maxsize > 0)
            {
                builder.Append(",").AppendField("maxsize", options.maxsize);
            }

            if (!string.IsNullOrEmpty(options.file))
            {
                throw new System.ArgumentException("The file property not supported.  Pass the captions in as a byte array.");
            }

            if (!string.IsNullOrEmpty(options.file_checksum))
            {
                builder.Append(",").AppendField("file_checksum", options.file_checksum);
            }

            // Either a video_id or video_reference_id is required
            if (options.video_id > 0)
            {
                builder.Append(",").AppendField("video_id", options.video_id);
            }
            else if (!string.IsNullOrEmpty(options.video_reference_id))
            {
                builder.Append(",").AppendField("video_reference_id", options.video_reference_id);
            }
            else
            {
                throw new System.ArgumentException("A video_id or video_reference_id is required for add_captioning");
            }

            RPCRequest rpc = new RPCRequest();

            rpc.method     = "add_captioning";
            rpc.parameters = builder.ToString();
            postParams.Add("json", rpc.ToJSON());

            postParams.Add("file", new UploadBufferParameter(captions, "captions.dfxp"));

            // Get the JSon reader returned from the APIRequest
            // RPCResponse<BCCaptionSources> rpcr = BCAPIRequest.ExecuteWriteFile<BCCaptionSources>(postParams, this.Account, @"C:\dev\svn\BrightCove\TestFormApp\sample_captions.dfxp");
            RPCResponse <BCCaptionSources> rpcr = BCAPIRequest.ExecuteWriteNew <BCCaptionSources>(postParams, this.Account);

            return(rpcr);
        }
        /// <summary>
        /// Upload a file to your Brightcove account
        /// </summary>
        /// <param name="video">
        /// The metadata for the video you'd like to create. This takes the form of a
        /// JSON object of name/value pairs, each of which corresponds to a settable
        /// property of the Video object.
        /// </param>
        /// <param name="filename">
        /// The name of the file that's being uploaded. You don't need to specify this in
        /// the JSON if it is specified in the file part of the POST.
        /// </param>
        /// <param name="file">
        /// A byte array of the video file you're uploading. This takes the
        /// form of a file part, in a multipart/form-data HTTP request. This input stream and
        /// the filename and maxSide parameters are automatically inferred from that file part.
        /// </param>
        /// <param name="encode_to">
        /// If the file requires transcoding, use this parameter to specify the target encoding. Valid
        /// values are MP4 or FLV, representing the H264 and VP6 codecs respectively. Note that transcoding
        /// of FLV files to another codec is not currently supported. This parameter is optional and defaults to FLV.
        /// </param>
        /// <param name="create_multiple_renditions">
        /// If the file is a supported transcodeable type, this optional flag can be used to control the
        /// number of transcoded renditions. If true (default), multiple renditions at varying encoding
        /// rates and dimensions are created. Setting this to false will cause a single transcoded VP6
        /// rendition to be created at the standard encoding rate and dimensions.
        /// </param>
        /// <param name="H264NoProcessing">
        /// If the video file is H.264 encoded and if create_multiple_ renditions=true, then multiple
        /// VP6 renditions are created and in addition the H.264 source is retained as an additional rendition.
        /// </param>
        /// <param name="preserve_source_rendition">
        /// Use this option to prevent H.264 source files from being transcoded. This parameter cannot be
        /// used in combination with create_multiple_renditions. It is optional and defaults to false.
        /// </param>
        /// <param name="maxsize">
        /// The maximum size that the file will be. This is used as a limiter to know when
        /// something has gone wrong with the upload. The maxSize is same as the file you uploaded.
        /// You don't need to specify this in the JSON if it is specified in the file part of the POST.
        /// </param>
        /// <param name="file_checksum">
        /// An optional MD5 hash of the file. The checksum can be used to verify that the file checked
        /// into your Brightcove Media Library is the same as the file you uploaded.
        /// </param>
        /// <returns>
        /// The id of the video that's been created. if null or error returns -1
        /// </returns>
        private RPCResponse <long> CreateVideo(BCVideo video, string filename, byte[] file, BCEncodeType encode_to, bool create_multiple_renditions,
                                               bool H264NoProcessing, bool preserve_source_rendition, long maxsize, string file_checksum, string fileFullPath)
        {
            // Generate post objects
            Dictionary <string, object> postParams = new Dictionary <string, object>();

            //add video to the post params
            RPCRequest rpc = new RPCRequest();

            rpc.method     = "create_video";
            rpc.parameters = "\"video\": " + video.ToCreateJSON() + ", \"token\": \"" + Account.WriteToken.Value + "\"";
            if (maxsize > -1)
            {
                rpc.parameters += ", \"maxsize\": " + maxsize.ToString();
            }
            if (file_checksum != null)
            {
                rpc.parameters += ", \"file_checksum\": \"" + file_checksum + "\"";
            }
            if (!encode_to.Equals(BCEncodeType.UNDEFINED))
            {
                rpc.parameters += ", \"encode_to\": " + encode_to.ToString();
            }
            rpc.parameters += ", \"create_multiple_renditions\": " + create_multiple_renditions.ToString().ToLower();
            rpc.parameters += ", \"H264NoProcessing\": " + H264NoProcessing.ToString().ToLower();
            rpc.parameters += ", \"preserve_source_rendition\": " + preserve_source_rendition.ToString().ToLower();
            postParams.Add("json", rpc.ToJSON());

            //add the file to the post
            if (!string.IsNullOrEmpty(filename))
            {
                rpc.parameters += ", \"filename\": \"" + filename + "\"";
            }
            if (!string.IsNullOrEmpty(filename) && file != null)
            {
                postParams.Add("file", new FileParameter(file, filename));
            }
            else if (!string.IsNullOrEmpty(fileFullPath))
            {
                postParams.Add("file", new UploadFileParameter(fileFullPath));
            }

            //Get the JSon reader returned from the APIRequest
            RPCResponse rpcr = string.IsNullOrEmpty(fileFullPath) ?
                               BCAPIRequest.ExecuteWrite(postParams, this.Account) :
                               BCAPIRequest.ExecuteWriteNew(postParams, this.Account);

            RPCResponse <long> rpcr2 = new RPCResponse <long>();

            rpcr2.error = rpcr.error;
            rpcr2.id    = rpcr.id;
            if (!string.IsNullOrEmpty(rpcr.result))
            {
                rpcr2.result = long.Parse(rpcr.result);
            }
            else
            {
                rpcr2.result = -1;
            }

            return(rpcr2);
        }