예제 #1
0
        /// <summary>
        /// 音频合成(要注意音频顺序,只支持MP3格式)
        /// </summary>
        /// <param name="data">
        /// [{"mp3":"xxxxxxxxx","topsize":0},{"mp3":"xxxxxxxxx","topsize":1},{"mp3":"xxxxxxxxx","topsize":2}
        /// ,{"mp3":"xxxxxxxxx","topsize":3},{"mp3":"xxxxxxxxx","topsize":4}]
        /// </param>
        /// <returns></returns>
        public ApiResult ComposeAudios(dynamic data)
        {
            ApiResult     apiResult      = new ApiResult();
            List <string> local_mp3_list = new List <string>();
            string        output         = "";

            try
            {
                if (!Util.isNotNull(data))
                {
                    return(new Models.ApiResult()
                    {
                        success = false,
                        message = "参数错误"
                    });
                }
                string folder = System.AppDomain.CurrentDomain.BaseDirectory + "download/";
                //获取formdata数据
                if (!System.IO.Directory.Exists(Path.GetDirectoryName(folder)))
                {
                    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder));
                }
                var            _mp3 = new { mp3 = "", topsize = 0 };
                List <dynamic> list = new List <dynamic>();
                list.Add(_mp3);
                dynamic[] _mp3_data_list = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(data.ToString(), list.ToArray());
                var       mp3_list       = _mp3_data_list.OrderBy(o => o.topsize).Select(o => o.mp3);
                string    file_type      = "";
                foreach (var mp3 in mp3_list)
                {
                    string mp3_file = mp3.ToString();
                    if (!Path.GetExtension(mp3_file).ToLower().Contains("mp3"))
                    {
                        return(new ApiResult()
                        {
                            success = false,
                            message = "只支持MP3格式的文件拼接"
                        });
                    }
                    file_type = Path.GetExtension(mp3.ToString());
                    string save_path = folder + Path.GetFileName(mp3.ToString());
                    string local_mp3 = UploadFile.DownloadFile(save_path.Replace("/", "\\"), mp3.ToString()).Result;
                    local_mp3_list.Add(local_mp3);
                }
                output = folder + sys.getRandomStr() + "_output" + file_type;
                if (FfmpegHelper.ComposeAudios(local_mp3_list.ToArray(), ref output))
                {
                    apiResult.data    = UploadFile.PostFile(output);
                    apiResult.success = true;
                    apiResult.message = "合成成功";
                }
                else
                {
                    return(new ApiResult()
                    {
                        success = false,
                        message = "合成失败"
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ApiResult()
                {
                    success = false,
                    message = ex.Message
                });
            }
            finally
            {
                if (local_mp3_list.Count > 0)
                {
                    foreach (string mp3 in local_mp3_list)
                    {
                        File.Delete(mp3);
                    }
                }
                if (Util.isNotNull(output))
                {
                    File.Delete(output);
                }
            }
            return(apiResult);
        }