简体   繁体   中英

Transcode audio from stereo .mp4 file into mono WAV 16KHz using MediaComposition

I am trying to use the Media Composition and Windows.Media.Transcoding APIs to automatically convert the audio track from some .mp4 files I have into mono PCM WAV audio at 16KHz in order to use the Microsoft Speech cognitive services (Speech to text).

I have a sample audio file with the right MEdiaEncodingProfile and I use MediaEncodingProfile.CreateFromFileAsync(sampleAudio) .

After that, I set up a transcoder and then use

PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(SourceVideo.VideoFile, tempFile, profile);

... but that results in prepareOp.CanTranscode = false since I do not think I can directly transcode a .mp4 into an audio file.

Is there a way for me to just grab a reference to the left audio track in an .mp4 file and then transcode that out into a wav file?

but that results in prepareOp.CanTranscode = false since I do not think I can directly transcode a .mp4 into an audio file.

Actually, you could converter video to audio with MediaTranscoder directly. You could refer the following and it works in my test.

MediaEncodingProfile profile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await
    transcoder.PrepareFileTranscodeAsync(source, destination, profile);
if (prepareOp.CanTranscode)
{
    var transcodeOp = prepareOp.TranscodeAsync();

    transcodeOp.Progress +=
        new AsyncActionProgressHandler<double>(TranscodeProgress);
    transcodeOp.Completed +=
        new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
    switch (prepareOp.FailureReason)
    {
        case TranscodeFailureReason.CodecNotFound:
            System.Diagnostics.Debug.WriteLine("Codec not found.");
            break;
        case TranscodeFailureReason.InvalidProfile:
            System.Diagnostics.Debug.WriteLine("Invalid profile.");
            break;
        default:
            System.Diagnostics.Debug.WriteLine("Unknown failure.");
            break;
    }
}

The only difference is the MediaEncodingProfile is created with MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low) not from sampleAudio . And I also tried custom PCM AudioEncodingProperties but it does not work. I suggest you use internal profile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM