/// <summary> /// Extract the video clip. /// </summary> private void btnExtract_Click(object sender, EventArgs e) { try { // Check parameters ... string sourceAudioFile = this.player.URL; if (string.IsNullOrEmpty(sourceAudioFile)) { throw new ArgumentException("Please choose the source audio file"); } string outputDirectory = this.tbOutputDirectory.Text; if (string.IsNullOrEmpty(outputDirectory)) { throw new ArgumentException("Please specify the output directory"); } string startpoint = this.tbStartpoint.Text; if (string.IsNullOrEmpty(tbStartpoint.Text)) { throw new ArgumentException("Please specify the startpoint of the audio clip"); } string endpoint = this.tbEndpoint.Text; if (string.IsNullOrEmpty(endpoint)) { throw new ArgumentException("Please specify the endpoint of the audio clip"); } // Extract the audio file. OutputAudioType outputType = (OutputAudioType)this.cmbOutputAudioType.SelectedValue; string outputFileName = ExpressionEncoderWrapper.ExtractAudio( sourceAudioFile, outputDirectory, outputType, Double.Parse(startpoint), Double.Parse(endpoint)); MessageBox.Show("Audio file is successfully extracted: " + outputFileName); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
/// <summary> /// Extract audio file. /// </summary> /// <param name="sourceAudioFile">The full path of the source audio file</param> /// <param name="outputDirectory">The output directory</param> /// <param name="outputAudioType">Output audio format (.mp4/.wma)</param> /// <param name="startpoint"> /// The extracting startpoint in the source audio file in milliseconds /// </param> /// <param name="endpoint"> /// The extracting endpoint in the source audio file in milliseconds /// </param> /// <returns>The full path of the output file name</returns> public static string ExtractAudio(string sourceAudioFile, string outputDirectory, OutputAudioType outputAudioType, double startpoint, double endpoint) { using (Job job = new Job()) { MediaItem src = new MediaItem(sourceAudioFile); switch (outputAudioType) { case OutputAudioType.MP4: src.OutputFormat = new MP4OutputFormat(); src.OutputFormat.AudioProfile = new AacAudioProfile(); src.OutputFormat.AudioProfile.Codec = AudioCodec.AAC; src.OutputFormat.AudioProfile.BitsPerSample = 24; break; case OutputAudioType.WMA: src.OutputFormat = new WindowsMediaOutputFormat(); src.OutputFormat.AudioProfile = new WmaAudioProfile(); src.OutputFormat.AudioProfile.Bitrate = new VariableConstrainedBitrate(128, 192); src.OutputFormat.AudioProfile.Codec = AudioCodec.WmaProfessional; src.OutputFormat.AudioProfile.BitsPerSample = 24; break; } TimeSpan spanStart = TimeSpan.FromMilliseconds(startpoint); src.Sources[0].Clips[0].StartTime = spanStart; TimeSpan spanEnd = TimeSpan.FromMilliseconds(endpoint); src.Sources[0].Clips[0].EndTime = spanEnd; job.MediaItems.Add(src); job.OutputDirectory = outputDirectory; job.Encode(); return(job.MediaItems[0].ActualOutputFileFullPath); } }