DotNet call Ali cloud media transcoding service

The company put the project on the part of the file Ali cloud OSS, some of which are amr audio file type, while the latter uses much trouble, so need to convert mp3 files for later use. Had wanted to use ffmpeg process, but the files are stored on Ali cloud OSS, using ffmpeg will need to file downloaded from a remote, after transcoding and re returns on Ali cloud, but also need to use the message components notification and transcoding It will be great pressure on the server. Ali cloud as a direct use of media transcoding service to the quick, Ali cloud provides only the OSS DotNet library, and does not provide MTS, it can only refer to its own API libraries and other languages ​​to develop, but fortunately the MTS API is not very complicated, after several attempts to get.

Relevant parameters need to obtain from Ali cloud console.

Ali cloud media transcoding service categories:

   /// <summary>
    /// 阿里云媒体转码服务助手类。
    /// </summary>
    public class MediaTranscodeHelper
    {

        private static readonly Encoding ENCODE_TYPE = Encoding.UTF8;
        private static readonly string ALGORITHM = "HmacSHA1";
        private static readonly string HTTP_METHOD = "GET";
        private static readonly string SEPARATOR = "&";
        private static readonly string EQUAL = "=";
        private static readonly string ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
        private static readonly string RegionId = "cn-beijing";
        private static readonly string Version = "2014-06-18";
        private static readonly string Format = "JSON";






        /// <returns>输出文件。</returns>
        public static string SubmitTranscodeJob(string inputFile, string ossLocation)
        {
            string outputJob = string.Empty;

            return outputJob;
        }

        /// <summary>
        /// 提交转码任务。
        /// </summary>
        public async Task<(bool success, string response)> SubmitTranscodeJob()
        {
            string SignatureNonce = Guid.NewGuid().ToString();
            var paramers = new Dictionary<string, string>
            {
                { "Action", Action },
                { "Version", "2014-06-18" },
                { "AccessKeyId", AccessKeyId },
                { "Timestamp", FormatIso8601Date(DateTime.Now) },
                { "SignatureMethod", "HMAC-SHA1" },
                { "SignatureVersion", "1.0" },
                { "SignatureNonce", SignatureNonce },
                { "Format", Format },
                { "PipelineId", PipelineId },
                { "Input", "{\"Bucket\":\"charlesbeijng\",\"Location\":\"oss-cn-beijing\",\"Object\":\"3.amr\"}" },
                { "OutputBucket", "charlesbeijng" },
                { "OutputLocation", "oss-cn-beijing" },
                { "Outputs", " [{\"OutputObject\":\"" + Guid.NewGuid().ToString() + ".mp3\",\"TemplateId\":\"1a94dc364cec44708f00367938a0122f\",\"Location\":\"oss-cn-beijing\",\"WaterMarks\":[{\"InputFile\":{\"Bucket\":\"charlesbeijng\",\"Location\":\"oss-cn-beijing\",\"Object\":\"1.png\"},\"WaterMarkTemplateId\":\"c473de87d0504f44be7ebdac1667ab13\"}]}]" }
            };

            try
            {
                string url = GetSignUrl(paramers, AccessKeySecret);

                int retryTimes = 1;
                var reply = await HttpGetAsync(url);
                while (500 <= reply.StatusCode && AutoRetry && retryTimes < MaxRetryNumber)
                {
                    url = GetSignUrl(paramers, AccessKeySecret);
                    reply = await HttpGetAsync(url);
                    retryTimes++;
                }

                if (!string.IsNullOrEmpty(reply.response))
                {
                    var res = JsonConvert.DeserializeObject<Dictionary<string, string>>(reply.response);
                    if (res != null && res.ContainsKey("Code") && "OK".Equals(res["Code"]))
                    {
                        return (true, reply.response);
                    }
                }

                return (false, reply.response);
            }
            catch (Exception ex)
            {
                return (false, response: ex.Message);
            }
        }

        /// <summary>
        /// 同步请求。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static string HttpGet(string url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = HTTP_METHOD;
            req.KeepAlive = true;
            req.UserAgent = "idui1";
            req.Timeout = TimeoutInMilliSeconds;
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            HttpWebResponse rsp = null;
            try
            {
                rsp = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException webEx)
            {
                if (webEx.Status == WebExceptionStatus.Timeout)
                {
                    rsp.Close();
                }
            }

            if (rsp != null)
            {
                if (rsp.CharacterSet != null)
                {
                    Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
                    return GetResponseAsString(rsp, encoding);
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 异步请求。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private async Task<(int StatusCode, string response)> HttpGetAsync(string url)
        {
            HttpClientHandler handler = new HttpClientHandler();
            = null handler.Proxy; 
            handler.AutomaticDecompression = DecompressionMethods.GZip; 

            the using (var = new new HTTP HttpClient (Handler)) 
            { 
                http.Timeout the TimeSpan new new = (* TimeSpan.TicksPerMillisecond TimeoutInMilliSeconds); 
                HttpResponseMessage the await http.GetAsync Response = (URL) ; 
                return ((int) Response.StatusCode, the await response.Content.ReadAsStringAsync ()); 
            }
        }

        /// <Summary> 
        /// the response stream is converted to text. 
        /// </ Summary> 
        /// <param name = "RSP"> response stream object </ param> 
        /// <param name = "encoding"> coding </ param> 
        /// <Returns> response text </ returns> 
        Private static String GetResponseAsString (the HttpWebResponse RSP, Encoding encoding) 
        { 
            the StringBuilder the StringBuilder new new Result = (); 
            Stream Stream = null; 
            Reader = null the StreamReader; 

            the try 
            { 

                // read the character stream in a manner HTTP response 
                stream rsp.GetResponseStream = (); 
                //rsp.Close (); 
                Reader new new = the StreamReader (stream, encoding); 

                // for each reading not more than 256 characters, and writes the string 
                char [] buffer = new char [ 256]; 
                int = readBytes 0; 
                the while ((readBytes = reader.Read (Buffer, 0, buffer.Length))> 0)
                {
                    result.Append(buffer, 0, readBytes);
                }
            }
            catch (WebException webEx)
            {
                if (webEx.Status == WebExceptionStatus.Timeout)
                {
                    result = new StringBuilder();
                }
            }
            finally
            {
                // 释放资源
                if (reader != null) reader.Close();
                if (stream != null) stream.Close();
                if (rsp != null) rsp.Close();
            }

            return result.ToString();
        }

        /// <summary>
        /// 处理消息。
        /// </summary>
        /// <param name="message">消息内容。</param>
        public static void HandlingMessage(string message)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        private static string FormatIso8601Date(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.CreateSpecificCulture("en-US"));
        /// </ summary>
        /// signature
        }


        /// <summary>
        public static string SignString(string source, string accessSecret)
        {
            using (var algorithm = new HMACSHA1(Encoding.UTF8.GetBytes(accessSecret.ToCharArray())))
            {
                return Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(source.ToCharArray())));
            }
        }


        private string GetSignUrl(Dictionary<string, string> parameters, string accessSecret)
        {
            var imutableMap = new Dictionary<string, string>(parameters)
            {
                //{ "Timestamp", FormatIso8601Date(DateTime.Now) },
                //{ "SignatureMethod", "HMAC-SHA1" },
                //{ "SignatureVersion", "1.0" },
                //{ "SignatureNonce", Guid.NewGuid().ToString() },
                //{ "Action", Action },
                //{ "Version", Version },
                //{ "Format", Format },
                //{ "RegionId", RegionId }
            };

            IDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>(imutableMap, StringComparer.Ordinal);
            StringBuilder canonicalizedQueryString = new StringBuilder();
            foreach (var p in sortedDictionary)
            {
                canonicalizedQueryString.Append("&")
                .Append(PercentEncode(p.Key)).Append("=")
                .Append(PercentEncode(p.Value));
            }

            StringBuilder stringToSign = new StringBuilder();
            stringToSign.Append(HTTP_METHOD);
            stringToSign.Append(SEPARATOR);
            stringToSign.Append(PercentEncode("/"));
            stringToSign.Append(SEPARATOR);
            stringToSign.Append(PercentEncode(canonicalizedQueryString.ToString().Substring(1)));

            string signature = SignString(stringToSign.ToString(), accessSecret + "&");

            imutableMap.Add("Signature", signature);

            return ComposeUrl(MtsDomain, imutableMap);
        }
        private static string ComposeUrl(string endpoint, Dictionary<String, String> parameters)
        {
            StringBuilder urlBuilder = new StringBuilder("");
            urlBuilder.Append("http://").Append(endpoint);
            if (-1 == urlBuilder.ToString().IndexOf("?"))
            {
                urlBuilder.Append("/?");
            }
            string query = ConcatQueryString(parameters);
            return urlBuilder.Append(query).ToString();
        }
        private static string ConcatQueryString(Dictionary<string, string> parameters)
        {
            if (null == parameters)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();

            foreach (var entry in parameters)
            {
                String key = entry.Key;
                String val = entry.Value;

                sb.Append(HttpUtility.UrlEncode(key, Encoding.UTF8));
                if (val != null)
                {
                    sb.Append("=").Append(HttpUtility.UrlEncode(val, Encoding.UTF8));
                }
                sb.Append("&");
            }

            int strIndex = sb.Length;
            if (parameters.Count > 0)
                sb.Remove(strIndex - 1, 1);

            return sb.ToString();
        }

        public static string PercentEncode(string value)
        {
            StringBuilder stringBuilder = new StringBuilder();
            string text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
            byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(value);
            foreach (char c in bytes)
            {
                if (text.IndexOf(c) >= 0)
                {
                    stringBuilder.Append(c);
                }
                else
                {
                    stringBuilder.Append("%").Append(
                        string.Format(CultureInfo.InvariantCulture, "{0:X2}",(Int) c));
                }
            }
            return stringBuilder.ToString();
        }
        /// <summary>
        /// HMAC-SHA1加密算法
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="input">要加密的串</param>
        /// <returns></returns>
        public static string HmacSha1(string key, string input)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            HMACSHA1 hmac = new HMACSHA1(keyBytes);
            byte[] hashBytes = hmac.ComputeHash(inputBytes);
            return Convert.ToBase64String(hashBytes);
        }
        /// <summary>
        /// AES encryption algorithm (ECB Mode) encrypts the plaintext, the encrypted base64 encoded, returns the ciphertext 
        /// </ Summary> 
        /// <param name = "EncryptStr "> plain </ param>
        /// <param name="Key">密钥</param>
        /// <returns>加密后base64编码的密文</returns>
        public static string Encrypt(string EncryptStr, string Key)
        {
            try
            {
                //byte[] keyArray = Encoding.UTF8.GetBytes(Key);
                byte[] keyArray = Convert.FromBase64String(Key);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(EncryptStr);

                RijndaelManaged rDel = new RijndaelManaged
                {
                    Key = keyArray,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };

                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static string Decrypt(string toDecrypt, string key)
        {
            byte[] keyArray = Convert.FromBase64String(key); // 将 TestGenAESByteKey 类输出的字符串转为 byte 数组
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            RijndaelManaged rDel = new RijndaelManaged
            {
                Key = keyArray,
                Mode = CipherMode.ECB,        // 必须设置为 ECB
                Padding = PaddingMode.PKCS7  // 必须设置为 PKCS7
            };
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }
        private static string BuildCanonicalizedQueryString(Dictionary<string, string> parameters)
        {
            // 对参数进行排序
            List<string> sortedKeys = new List<string>(parameters.Keys);
            sortedKeys.Sort();

            StringBuilder temp = new StringBuilder();

            foreach (var key in sortedKeys)
            {
                // 此处需要对 key 和 value 进行编码
                string value = parameters[key];
                temp.Append(SEPARATOR).Append(PercentEncode(key)).Append(EQUAL).Append(PercentEncode(value));
            }
            return temp.ToString().Substring(1);
        }


        private static string BuildRequestURL(string signature, Dictionary<string, string> parameters)
        {
            // 生成请求 URL
            StringBuilder temp = new StringBuilder("mts.cn-beijing.aliyuncs.com");
            temp.Append(HttpUtility.UrlEncode("Signature", ENCODE_TYPE)).Append("=").Append(signature);
            foreach (var item in parameters)
            {
                temp.Append("&").Append(PercentEncode(item.Key)).Append("=").Append(PercentEncode(item.Value));
            }
            return temp.ToString();
        }
    }

When using a method called directly SubmitTranscodeJob on it.

Guess you like

Origin www.cnblogs.com/weisenz/p/11308806.html