public MediaStreamInfo ToStreamInfo() { MediaStreamInfo stream = null; if (CodecType == "audio") { stream = new AudioStreamInfo { ChannelLayout = this.ChannelLayout, ChannelCount = this.ChannelCount, SampleFormat = FfmpegHelper.ParseSampleFormat(SampleFormat), SampleRate = SampleRate, }; } else if (CodecType == "video") { stream = new VideoStreamInfo { PixelFormat = PixelFormatHelper.Parse(PixelFormat), Width = this.Width, Height = this.Height }; if (Tags != null && Tags.TryGetValue("rotate", out var rotate)) { ((VideoStreamInfo)stream).Rotate = (int)rotate; } } else if (CodecType == "subtitle") { stream = new SubtitleStreamInfo { }; } else if (CodecType == "data") { stream = new DataStreamInfo { }; } else { stream = new MediaStreamInfo(); } stream.Codec = Profile != null?CodecInfo.Create(CodecIdHelper.Parse(CodecName), Profile).Name : CodecName; if (TimeBase != null && Rational.TryParse(TimeBase, out var timeBase)) { stream.TimeBase = timeBase; } stream.Duration = Duration ?? TimeSpan.Zero; stream.StartTime = StartTime ?? TimeSpan.Zero; stream.FrameCount = FrameCount; return(stream); }
public void Crop(string input, string output) { string start = string.Empty; string end = string.Empty; try { // Call 'ValidateText().ToString()' because it will throw a // exception if there is a error in the text. mtxtFrom.ValidateText().ToString(); start = mtxtFrom.Text; if (chbCropTo.Enabled && chbCropTo.Checked) { mtxtTo.ValidateText().ToString(); end = mtxtTo.Text; } } catch { MessageBox.Show(this, "Cropping information error."); return; } CroppingListViewItem item = new CroppingListViewItem(Path.GetFileName(output)); item.SubItems.Add(""); item.SubItems.Add("Cropping"); item.SubItems.Add(FormatVideoLength(FfmpegHelper.GetDuration(input))); item.SubItems.Add(GetFileSize(input)); item.SubItems.Add(""); lvQueue.Items.Add(item); SelectOneItem(item); ProgressBar pb = new ProgressBar() { Maximum = 100, Minimum = 0, Value = 0 }; lvQueue.AddEmbeddedControl(pb, 1, item.Index); LinkLabel ll = new LinkLabel() { Text = Path.GetFileName(input), Tag = input }; ll.LinkClicked += linkLabel_LinkClicked; lvQueue.AddEmbeddedControl(ll, 5, item.Index); item.Crop(input, output, start, end); }
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.SubItems[2].Text = "Success"; this.SubItems[3].Text = MainForm.FormatVideoLength(FfmpegHelper.GetDuration(this.Input)); this.SubItems[4].Text = MainForm.GetFileSize(this.Output); this.Status = OperationStatus.Success; Program.RunningWorkers.Remove(backgroundWorker); OnOperationComplete(new OperationEventArgs(this, this.Status)); }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (cropEnd == string.Empty) { FfmpegHelper.Crop(backgroundWorker, this.Input, this.Output, cropStart); } else { FfmpegHelper.Crop(backgroundWorker, this.Input, this.Output, cropStart, cropEnd); } cropStart = cropEnd = string.Empty; }
private async Task PublishLive() { if (dgvLivebroadcast.SelectedRows == null) { NotificationHelper.Notice("Chưa chọn livestream để publish"); } else { var currentSelectedLive = dgvLivebroadcast.SelectedRows[0].DataBoundItem as LivestreamBroadcast; if (currentSelectedLive != null) { if (NotificationHelper.Confirm("Bạn có muốn publish livestream này?") == DialogResult.Yes) { foreach (var media in DataHelper.GetMedias(currentSelectedLive.Playlist.PlaylistId)) { switch (media.MediaTypeId) { case 1: LiveFromMediaSource(media.MediaLink, currentSelectedLive.Format, currentSelectedLive.Fps, currentSelectedLive.LivestreamName); break; case 2: await LiveFromMediaLink(media.MediaLink, currentSelectedLive.Format, currentSelectedLive.Fps, currentSelectedLive.LivestreamName); break; case 3: LiveFromLiveStream(media.MediaLink, currentSelectedLive.Format, currentSelectedLive.Fps, currentSelectedLive.LivestreamName); break; default: LogHelper.AddLog("Media in playlist can't map type"); break; } var result = await YoutubeApiHelper.GoLiveBroadcast(currentSelectedLive.LiveStreamBroadcastId, currentSelectedLive.LiveStreamId); if (!result) { NotificationHelper.Notice("Không thể publish livestream"); FfmpegHelper.StopFFmpegProcess(currentSelectedLive.LivestreamName); LogHelper.AddLog("Stop ffmpeg process"); } } } } } }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { FfmpegHelper.Convert(backgroundWorker, this.Input, this.Output); if (!string.IsNullOrEmpty(converterStart)) { if (string.IsNullOrEmpty(converterEnd)) { FfmpegHelper.Crop(backgroundWorker, this.Output, this.Output, converterStart); } else { FfmpegHelper.Crop(backgroundWorker, this.Output, this.Output, converterStart, converterEnd); } } this.converterStart = this.converterEnd = string.Empty; }
/// <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); }
public ApiResult speechEvaluation() { ApiResult apiResult = new ApiResult(); string speechFilePath = string.Empty; string silkFile = string.Empty; string otherFile = string.Empty; try { string fileName = sys.getRandomStr();//用作最终生成的文件名 List <string> speechResult = null; //接受小程序提交过来的录音文件 System.Web.HttpFileCollection fileList = HttpContext.Current.Request.Files; string enText = HttpContext.Current.Request.Form["enText"]; string lan = HttpContext.Current.Request.Form["language"]; enText = enText.Replace("\n", "").Replace("\r", ""); if (fileList.Count < 1) { return(new ApiResult() { success = false, message = "语音文件为空" }); } System.Web.HttpPostedFile file = fileList[0]; //判断文件是silk格式还是webm格式,不能通过文件后缀判断 StreamReader reader = new StreamReader(file.InputStream); string fileString = reader.ReadToEnd(); BaiduAI ai = new BaiduAI(); if (fileString.IndexOf("SILK_V3") > -1) { //silk格式文件 silkFile = CommonServices.createFileFullPath("silk"); file.SaveAs(silkFile); speechFilePath = CmdServices.silk2wav(silkFile); string fileType = CommonServices.getFileType(speechFilePath); //speechResult = ai.AsrData(speechFilePath, fileType, 16000, lan); FileInfo file_info = new FileInfo(speechFilePath); speechResult = Util.GetSpeechResult(file_info, lan); } else if (fileString.IndexOf("webm") > -1) { #region //webm格式文件 //微信小程序webm格式录音文件,data:audio/webm;base64,GkXfo59ChoEBQv... //1.去掉data:audio/webm;base64, //2.对GkXfo59ChoEBQv...字符串解码为webm文件 //3.webm文件转wav文件 #endregion LogHelper.Info("小程序音频评分开始:" + DateTime.Now); fileString = fileString.Substring(fileString.IndexOf(',') + 1); byte[] bytes = Convert.FromBase64String(fileString); string webmFile = CommonServices.createFileFullPath("webm"); using (FileStream fs = new FileStream(webmFile, FileMode.Create, FileAccess.Write)) { fs.Write(bytes, 0, bytes.Length); fs.Flush(); } speechFilePath = CmdServices.webm2wav(webmFile); string fileType = CommonServices.getFileType(speechFilePath); //speechResult = ai.AsrData(speechFilePath, fileType, 16000, lan); LogHelper.Info("小程序音频评分结束:" + DateTime.Now); FileInfo file_info = new FileInfo(speechFilePath); speechResult = Util.GetSpeechResult(file_info, lan); } else//mp3或者wav,如果不是16000HZ,16bit,单声道,都转格式 { string fileType = CommonServices.getFileType(file.FileName); otherFile = CommonServices.createFileFullPath(fileType); file.SaveAs(otherFile); //转换成wav\ LogHelper.Info("MP3转wav"); string mp3_folder = HttpContext.Current.Server.MapPath("/upload/") + sys.getDateStr() + "\\"; if (Directory.Exists(mp3_folder)) { Directory.CreateDirectory(mp3_folder); } speechFilePath = mp3_folder + sys.getRandomStr() + ".wav"; //speechFilePath = CmdServices.mp32wav(otherFile); FfmpegHelper.ChangeFileType(otherFile, speechFilePath); LogHelper.Info("百度转语音开始"); //speechResult = ai.AsrData(speechFilePath, CommonServices.getFileType(speechFilePath), 16000, lan); FileInfo file_info = new FileInfo(speechFilePath); speechResult = Util.GetSpeechResult(file_info, lan); } string duration = FileServices.getMediaDuration(speechFilePath); int seconds = SppechEvaluation.timeToSecond(duration); //评分 int totalSocre = 0; int accuracySocre = 0; int fluencySocre = 0; int integritySocre = 0; int tempScore = 0; string sentence = ""; //LogHelper.Info("百度转语音完成,准备计算分数。seconds:" + seconds+";count:"+ speechResult.Count); if (speechResult == null) { sentence = SppechEvaluation.getSentenceAccuracy3(enText, speechResult); apiResult.data = new { totalSocre, accuracySocre, fluencySocre, integritySocre, speechFilePath = UploadFile.PostFile(speechFilePath), sentence }; } else { //开始计算得分 for (int i = 0; i < speechResult.Count; i++) { if (lan == "zh") { char[] words = SppechEvaluation.getSentenceWordsZh(speechResult[i]); tempScore = SppechEvaluation.calaAccuracySocre(enText, words); if (tempScore > accuracySocre) { accuracySocre = tempScore; } tempScore = SppechEvaluation.calaFluencySocreZh(enText, seconds); if (tempScore > fluencySocre) { fluencySocre = tempScore; } tempScore = SppechEvaluation.calaIntegritySocre(enText, words, accuracySocre); if (tempScore > integritySocre) { integritySocre = tempScore; } sentence = SppechEvaluation.getSentenceAccuracyZh(enText, speechResult); } else { string[] words = SppechEvaluation.getSentenceWords(speechResult[i]); //accuracySocre、fluencySocre、integritySocre取平均分,totalSocre取accuracySocre最高分 tempScore = SppechEvaluation.calaAccuracySocre(enText, words); if (tempScore > accuracySocre) { accuracySocre = tempScore; } tempScore = SppechEvaluation.calaFluencySocre(enText, seconds); if (tempScore > fluencySocre) { fluencySocre = tempScore; } tempScore = SppechEvaluation.calaIntegritySocre(enText, words, accuracySocre); if (tempScore > integritySocre) { integritySocre = tempScore; } sentence = SppechEvaluation.getSentenceAccuracy2(enText, speechResult); } } totalSocre = (int)((float)(accuracySocre * 60 + fluencySocre * 20 + integritySocre * 20) / 100); if (speechResult.Count == 0) { sentence = SppechEvaluation.getSentenceAccuracy3(enText, speechResult); } //return "{\"success\":true,\"message\":\"\",\"data\":{\"totalSocre\":\"" + totalSocre + "\",\"accuracySocre\":\"" + accuracySocre + "\",\"fluencySocre\":\"" + fluencySocre + "\",\"integritySocre\":\"" + integritySocre + "\",\"speechFilePath\":\"" + FileServices.urlConvertor(speechFilePath) + "\",\"speechSeconds\":\"" + seconds.ToString() + "\",\"sentence\":" + sentence + "}}"; apiResult.data = new { totalSocre, accuracySocre, fluencySocre, integritySocre, speechFilePath = UploadFile.PostFile(speechFilePath), speechSeconds = seconds.ToString(), sentence }; } apiResult.success = true; apiResult.message = "获取成功"; } catch (Exception ex) { return(new ApiResult() { success = false, message = ex.Message }); } finally { //删掉服务器上的文件 if (Util.isNotNull(speechFilePath)) { if (File.Exists(speechFilePath)) { File.Delete(speechFilePath); } } if (Util.isNotNull(silkFile)) { if (File.Exists(silkFile)) { File.Delete(silkFile); } } if (Util.isNotNull(otherFile)) { if (File.Exists(otherFile)) { File.Delete(otherFile); } } } return(apiResult); }
public ApiResult CutAudio() { ApiResult apiResult = new Models.ApiResult(); List <string> output_list = new List <string>(); string input = string.Empty; try { Dictionary <string, string> cut_files = new Dictionary <string, string>(); HttpFileCollection fileList = HttpContext.Current.Request.Files; if (fileList.Count < 1) { return(new ApiResult() { success = false, message = "文件为空" }); } HttpPostedFile file = fileList[0]; string folder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/"; if (!System.IO.Directory.Exists(Path.GetDirectoryName(folder))) { System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder)); } //先保存文件 input = folder + "cutAudio_" + sys.getRandomStr() + Path.GetExtension(file.FileName); input = input.Replace("/", "\\"); file.SaveAs(input); string timespans = HttpContext.Current.Request.Form["timespans"]; string[] times = timespans.Split(new char[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries); string start = ""; string end = ""; List <string> cut_times = new List <string>(); foreach (string time in times) { if (Util.isNotNull(time)) { cut_times.Add(time); } } //没有结束时间,所以需要加上 string _all_audio_time = FfmpegHelper.getMediaDuration(input); string[] all_audio_times = _all_audio_time.Split(':'); string audio_times = (int.Parse(all_audio_times[0]) * 3600 + int.Parse(all_audio_times[1]) * 60 + double.Parse(all_audio_times[2])).ToString(); cut_times.Add(audio_times); //确保没有空数据 if (cut_times.Count > 1) { for (int i = 1; i < cut_times.Count; i++) { try { start = cut_times[i - 1]; TimeSpan _start = TimeSpan.FromSeconds(double.Parse(start)); end = cut_times[i]; TimeSpan _end = TimeSpan.FromSeconds(double.Parse(end) - double.Parse(start)); string output = folder + i + "_cutAudio_" + sys.getRandomStr() + Path.GetExtension(file.FileName); output = output.Replace("/", "\\"); output_list.Add(output); bool is_success = FfmpegHelper.CutAudioFile(input, output, _start, _end); //上传到服务器 if (File.Exists(output)) { string server_path = UploadFile.PostFile(output); cut_files.Add(start, server_path); } } catch (Exception ex) { return(new ApiResult() { success = false, message = "timespans参数不对" }); } } apiResult.success = true; apiResult.message = "截取成功"; apiResult.data = cut_files; } else { return(new ApiResult() { success = false, message = "timespans参数不对" }); } } catch (Exception ex) { return(new ApiResult() { success = false, message = ex.Message }); } finally { if (Util.isNotNull(input)) { if (File.Exists(input)) { File.Delete(input); } } foreach (string file in output_list) { if (Util.isNotNull(file)) { if (File.Exists(file)) { File.Delete(file); } } } } return(apiResult); }
private void LiveFromMediaSource(string mediaLink, string resolution, int fps, string outputRtmp) { FfmpegHelper.PushLivestreamFromLocalFile(mediaLink, resolution, fps, outputRtmp); }
private void LiveFromLiveStream(string mediaLink, string resolution, int fps, string outputRtmp) { var data = YoutubeDlHelper.GetMediaLivestreamLink(mediaLink); FfmpegHelper.PushLivestreamFromLivestream(data, resolution, fps, outputRtmp); }
private async Task LiveFromMediaLink(string mediaLink, string resolution, int fps, string outputRtmp) { var data = await YoutubeDlHelper.GetMediaStreamLink(mediaLink); FfmpegHelper.PushLivestreamFromMediaLink(data, resolution, fps, outputRtmp); }