1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pub use crate::bindings::windows::{
    foundation::AsyncActionProgressHandler,
    media::transcoding::MediaTranscoder,
};
use crate::{
    File,
    TranscodeError,
    TranscodeResult,
};
pub use windows_media_transcoding_bindings::windows::media::{
    media_properties::{
        AudioEncodingQuality,
        MediaEncodingProfile,
        VideoEncodingQuality,
    },
    transcoding::PrepareTranscodeResult,
};

/// A media profile used for transcoding. Cloning this produces another handle to it.
#[derive(Debug, Clone)]
pub struct MediaProfile {
    profile: MediaEncodingProfile,
}

impl MediaProfile {
    /// Create a HEVC profile with the given video quality.
    pub fn create_hevc(video_quality: VideoQuality) -> TranscodeResult<Self> {
        let profile = MediaEncodingProfile::create_hevc(video_quality.into())?;

        Ok(MediaProfile { profile })
    }

    /// Create an Mp3 profile with the given audio quality.
    pub fn create_mp3(audio_quality: AudioQuality) -> TranscodeResult<Self> {
        let profile = MediaEncodingProfile::create_mp3(audio_quality.into())?;

        Ok(MediaProfile { profile })
    }

    /// Get the inner [`MediaEncodingProfile`] for this object
    pub fn as_media_encoding_profile(&self) -> &MediaEncodingProfile {
        &self.profile
    }
}

/// A representation of video quality
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub enum VideoQuality {
    Auto,
    HD1080p,
    HD720p,
    Wvga,
    Ntsc,
    Pal,
    Vga,
    Qvga,

    // NOTE: May not be present on all platforms
    Uhd2160p,

    // Note: May not be present on all platforms
    Uhd4320p,
}

impl Into<VideoEncodingQuality> for VideoQuality {
    fn into(self) -> VideoEncodingQuality {
        match self {
            Self::Auto => VideoEncodingQuality::Auto,
            Self::HD1080p => VideoEncodingQuality::HD1080p,
            Self::HD720p => VideoEncodingQuality::HD720p,
            Self::Wvga => VideoEncodingQuality::Wvga,
            Self::Ntsc => VideoEncodingQuality::Ntsc,
            Self::Pal => VideoEncodingQuality::Pal,
            Self::Vga => VideoEncodingQuality::Vga,
            Self::Qvga => VideoEncodingQuality::Qvga,
            Self::Uhd2160p => VideoEncodingQuality::Uhd2160p,
            Self::Uhd4320p => VideoEncodingQuality::Uhd4320p,
        }
    }
}

/// A representation of audio quality
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub enum AudioQuality {
    Auto,
    High,
    Medium,
    Low,
}

impl Into<AudioEncodingQuality> for AudioQuality {
    fn into(self) -> AudioEncodingQuality {
        match self {
            Self::Auto => AudioEncodingQuality::Auto,
            Self::High => AudioEncodingQuality::High,
            Self::Medium => AudioEncodingQuality::Medium,
            Self::Low => AudioEncodingQuality::Low,
        }
    }
}

/// A Transcoding Operation thats ready to be started
pub struct PreparedTranscode {
    operation: PrepareTranscodeResult,
}

impl PreparedTranscode {
    /// Begin transcoding with a progress callback
    pub async fn transcode_with_progress<F: FnMut(f64) + 'static>(
        &self,
        mut f: F,
    ) -> TranscodeResult<()> {
        let operation = self.operation.transcode_async()?;
        let handler = AsyncActionProgressHandler::new(move |_handler, progress| {
            f(*progress);
            Ok(())
        });
        operation.set_progress(handler)?;

        operation.await?;

        Ok(())
    }

    /// Begin transcoding
    pub async fn transcode(&self) -> TranscodeResult<()> {
        self.operation.transcode_async()?.await?;

        Ok(())
    }
}

/// A Transcoder
#[derive(Debug, Clone)]
pub struct Transcoder {
    transcoder: MediaTranscoder,
}

impl Transcoder {
    /// Create a new transcoder
    pub fn new() -> TranscodeResult<Self> {
        let transcoder = MediaTranscoder::new()?;

        Ok(Transcoder { transcoder })
    }

    /// Prepare a transcode operation and verify it
    pub async fn prepare_transcode(
        &self,
        input_file: &File,
        output_file: &File,
        target_profile: &MediaProfile,
    ) -> TranscodeResult<PreparedTranscode> {
        let operation = self
            .transcoder
            .prepare_file_transcode_async(
                &input_file.file,
                &output_file.file,
                &target_profile.profile,
            )?
            .await?;

        if !operation.can_transcode()? {
            return Err(TranscodeError::PrepareTranscodeFailure(
                operation.failure_reason()?,
            ));
        }

        Ok(PreparedTranscode { operation })
    }

    /// Get the inner [`MediaTranscoder`] from this object.
    pub fn as_media_transcoder(&self) -> &MediaTranscoder {
        &self.transcoder
    }
}