public string ExtractAudioFromVideo(string outputPathWithoutExtension, string videoPath) { if (string.IsNullOrEmpty(outputPathWithoutExtension)) { throw new ArgumentNullException(nameof(outputPathWithoutExtension)); } if (string.IsNullOrEmpty(videoPath)) { throw new ArgumentNullException(nameof(videoPath)); } string outputPath = outputPathWithoutExtension + MediaTypeHelper.GetFileExtensionForAudioCodec(new MediaFileInfo(videoPath).AudioStreams[0].CodecName); if (System.IO.File.Exists(outputPath)) { System.IO.File.Delete(outputPath); } FFmpegProcessRunner fpr = new FFmpegProcessRunner(); fpr.OnDurationMessage += DurationMessageReceived; fpr.OnTimeMessage += TimeMessageReceived; totalSteps = 1; currentStep = 1; fpr.Run($"-i \"{videoPath}\" -vn -codec:a copy \"{outputPath}\""); fpr.OnDurationMessage -= DurationMessageReceived; fpr.OnTimeMessage -= TimeMessageReceived; return(outputPath); }
public void NormalizeVolume(string outputPath, string filePath) { if (string.IsNullOrEmpty(outputPath)) { throw new ArgumentNullException(nameof(outputPath)); } if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (System.IO.File.Exists(outputPath)) { System.IO.File.Delete(outputPath); } FFmpegProcessRunner fpr = new FFmpegProcessRunner(); fpr.OnDurationMessage += DurationMessageReceived; fpr.OnTimeMessage += TimeMessageReceived; totalSteps = 2; // First pass. currentStep = 1; string output = fpr.Run($"-i \"{filePath}\" -af loudnorm=I=-23:LRA=7:tp=-2:print_format=json -f null -"); int startIndex = output.LastIndexOf("{"); int length = output.LastIndexOf("}") - startIndex + 1; Dictionary <string, string> loudnormOutput = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(output.Substring(startIndex, length)); // Second pass. currentStep = 2; string vcodecArg = string.Empty; if (MediaTypeHelper.GetMediaTypeFromFileName(filePath) == MediaType.VIDEO) { vcodecArg = "-vcodec copy "; } fpr.Run($"-i \"{filePath}\" " + $"-af loudnorm=I=-23:LRA=7:tp=-2:" + $"measured_I={loudnormOutput["input_i"]}:" + $"measured_LRA={loudnormOutput["input_lra"]}:" + $"measured_tp={loudnormOutput["input_tp"]}:" + $"measured_thresh={loudnormOutput["input_thresh"]}:" + $"offset={loudnormOutput["target_offset"]}:" + $"linear=true:print_format=json {vcodecArg}{outputPath}"); fpr.OnDurationMessage -= DurationMessageReceived; fpr.OnTimeMessage -= TimeMessageReceived; }