mediaFoundationTranscode

Typecommand
DictionaryLCS
Librarymediafoundation
Syntax
mediaFoundationTranscode <pReaderFilePath>, <pWriterFilePath>, <pVideoProcessorID>, [<pOffset>], [<pFrom>], [<pDuration>]
Associationsmediafoundation
Summary

Transcode part or all of the media from from a reader to a writer Platform:desktop

OSwindows
Parameters
NameTypeDescription
pReaderFilePath

The path to a media file

pWriterFilePath

The path to a media file

pVideoProcessorID

The ide of a video processor

pOffset

An offset to write to a writer that already has content up to the offset. 100 nanosecond units

pFrom

Seek to this time before starting to transcode. 100 nanosecond units

pDuration

The duration to trancode where 0 means the entire file from pFrom. 100 nanosecond units

The result (c-string): An error string if an error occurred

Example
constant kMFVideoInterlace_Progressive = 2
constant kMFVideoInterlace_FieldInterleavedUpperFirst = 3
constant kMFVideoInterlace_FieldInterleavedLowerFirst = 4
constant kMFVideoInterlace_FieldSingleUpper = 5
constant kMFVideoInterlace_FieldSingleLower = 6
constant kMFVideoInterlace_MixedInterlaceOrProgressive = 7

command Transcode pReaderFilePath, pWriterFilePath
   mediaFoundationCreateReader pReaderFilePath
   mediaFoundationCreateWriter pWriterFilePath

   mediaFoundationCreateMediaType
   local tVideoMediaType
   put the result into tVideoMediaType

   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "major type", "media type video"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "subtype", "video format H264"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video frame size", "400,300"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video frame rate", "20000,1000"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video avg bitrate", 100000
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video interlace mode", kMFVideoInterlace_Progressive
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video aspect ratio", "1,1"

   mediaFoundationCreateMediaType
   local tAudioMediaType
   put the result into tAudioMediaType

   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "major type", "media type audio"
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "subtype", "audio format AAC"
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio bits per sample", 16
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio samples per second", 44100
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio num channels", 2
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio avg bytes per second", 16000
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio block alignment", 1

   local tStreamCount
   put mediaFoundationReaderNumberOfStreams(pReaderFilePath) into tStreamCount

   local tVideoProcessor
   mediaFoundationCreateVideoProcessor
   put the result into tVideoProcessor

   repeat with tIndex = 1 to tStreamCount
      local tMajorType
      local tNativeMediaType
      put mediaFoundationReaderStreamGetNativeMediaType(pReaderFilePath, tIndex) into tNativeMediaType
      put mediaFoundationMediaTypeGetAttribute(tNativeMediaType, "major type") into tMajorType

      mediaFoundationCreateMediaType
      local tPartialMediaType
      put the result into tPartialMediaType

      mediaFoundationMediaTypeSetAttribute tPartialMediaType, "major type", tMajorType

      local tFoundMatch
      switch tMajorType
         case "media type video"
            local tSize
            put mediaFoundationMediaTypeGetAttribute(tNativeMediaType, "video frame size") into tSize
            mediaFoundationVideoProcessorSetSourceRect tVideoProcessor, 0,0,400,300
            mediaFoundationVideoProcessorSetDestinationRect tVideoProcessor, 0,0,item 1 of tSize, item 2 of tSize

            mediaFoundationAddStreamToWriter pWriterFilePath, tVideoMediaType
            mediaFoundationDeleteMediaType tVideoMediaType

            put false into tFoundMatch

            local tInputMediaTypeIndex
            put 0 into tInputMediaTypeIndex

            local tAvailableInputMediaType
            local tCurrentMediaType
            repeat
               add 1 to tInputMediaTypeIndex
               put mediaFoundationVideoProcessorGetInputAvailableMediaType(tVideoProcessor, tInputMediaTypeIndex) \
                     into tAvailableInputMediaType
               if tAvailableInputMediaType is 0 then
                  exit repeat
               end if

               local tResult
               mediaFoundationReaderStreamSetCurrentMediaType pReaderFilePath, tIndex, tAvailableInputMediaType
               put the result into tResult
               if tResult then
                  put mediaFoundationReaderStreamGetCurrentMediaType(pReaderFilePath, tIndex) into tCurrentMediaType
                  mediaFoundationVideoProcessorSetInputMediaType tVideoProcessor, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if
                  mediaFoundationMediaTypeSetAttribute tCurrentMediaType, "video frame size", "400,300"

                  mediaFoundationVideoProcessorSetOutputMediaType tVideoProcessor, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if

                  mediaFoundationWriterStreamSetInputMediaType pWriterFilePath, tIndex, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if

                  mediaFoundationDeleteMediaType tCurrentMediaType
                  put true into tFoundMatch

               end if

               mediaFoundationDeleteMediaType tAvailableInputMediaType
               if tFoundMatch then
                  exit repeat
               end if
            end repeat

            if not tFoundMatch then
               exit repeat
            end if
            break
         case "media type audio"
            mediaFoundationAddStreamToWriter pWriterFilePath, tAudioMediaType
            mediaFoundationDeleteMediaType tAudioMediaType

            put false into tFoundMatch
            repeat for each item tIntermediateFormat in \
                  "audio format Float,audio format PCM"
               mediaFoundationMediaTypeSetAttribute tPartialMediaType, "subtype", tIntermediateFormat
               mediaFoundationReaderStreamSetCurrentMediaType pReaderFilePath, tIndex, tPartialMediaType
               if the result then
                  put mediaFoundationReaderStreamGetCurrentMediaType(pReaderFilePath, tIndex) into tCurrentMediaType
                  mediaFoundationWriterStreamSetInputMediaType pWriterFilePath, tIndex, tCurrentMediaType
                  put the result into tResult
                  mediaFoundationDeleteMediaType tCurrentMediaType
                  if tResult then
                     put true into tFoundMatch
                     exit repeat
                  end if
               end if
            end repeat
            if not tFoundMatch then
               exit repeat
            end if
            break
         default

      end switch

      mediaFoundationDeleteMediaType tPartialMediaType
      mediaFoundationDeleteMediaType tNativeMediaType
   end repeat

   if tFoundMatch then
      mediaFoundationWriterBeginWriting pWriterFilePath
      mediaFoundationTranscode pReaderFilePath, pWriterFilePath, tVideoProcessor, 0,0,0
      mediaFoundationWriterFinalizeWriting pWriterFilePath
   end if

   mediaFoundationDeleteReader pReaderFilePath
   mediaFoundationDeleteWriter pWriterFilePath
   mediaFoundationDeleteVideoProcessor tVideoProcessor
end Transcode

on mediaFoundationTranscoding pTimestamp
   put pTimestamp
end mediaFoundationTranscoding
constant kMFVideoInterlace_Progressive = 2
constant kMFVideoInterlace_FieldInterleavedUpperFirst = 3
constant kMFVideoInterlace_FieldInterleavedLowerFirst = 4
constant kMFVideoInterlace_FieldSingleUpper = 5
constant kMFVideoInterlace_FieldSingleLower = 6
constant kMFVideoInterlace_MixedInterlaceOrProgressive = 7

command Concatenate pReader1FilePath, pReader2FilePAth, pWriterFilePath
   mediaFoundationCreateReader pReader1FilePath
   mediaFoundationCreateReader pReader2FilePath
   mediaFoundationCreateWriter pWriterFilePath

   mediaFoundationCreateMediaType
   local tVideoMediaType
   put the result into tVideoMediaType

   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "major type", "media type video"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "subtype", "video format H264"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video frame size", "400,300"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video frame rate", "20000,1000"
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video avg bitrate", 100000
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video interlace mode", kMFVideoInterlace_Progressive
   mediaFoundationMediaTypeSetAttribute tVideoMediaType, "video aspect ratio", "1,1"

   mediaFoundationCreateMediaType
   local tAudioMediaType
   put the result into tAudioMediaType

   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "major type", "media type audio"
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "subtype", "audio format AAC"
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio bits per sample", 16
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio samples per second", 44100
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio num channels", 2
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio avg bytes per second", 16000
   mediaFoundationMediaTypeSetAttribute tAudioMediaType, "audio block alignment", 1

   local tStreamCount
   put mediaFoundationReaderNumberOfStreams(pReader1FilePath) into tStreamCount

   local tVideoProcessor
   mediaFoundationCreateVideoProcessor
   put the result into tVideoProcessor

   repeat with tIndex = 1 to tStreamCount
      local tMajorType
      local tNativeMediaType
      put mediaFoundationReaderStreamGetNativeMediaType(pReader1FilePath, tIndex) into tNativeMediaType
      put mediaFoundationMediaTypeGetAttribute(tNativeMediaType, "major type") into tMajorType

      mediaFoundationCreateMediaType
      local tPartialMediaType
      put the result into tPartialMediaType

      mediaFoundationMediaTypeSetAttribute tPartialMediaType, "major type", tMajorType

      local tFoundMatch
      switch tMajorType
         case "media type video"
            local tSize
            put mediaFoundationMediaTypeGetAttribute(tNativeMediaType, "video frame size") into tSize
            mediaFoundationVideoProcessorSetSourceRect tVideoProcessor, 0,0,400,300
            mediaFoundationVideoProcessorSetDestinationRect tVideoProcessor, 0,0,item 1 of tSize, item 2 of tSize

            mediaFoundationAddStreamToWriter pWriterFilePath, tVideoMediaType
            mediaFoundationDeleteMediaType tVideoMediaType

            put false into tFoundMatch

            local tInputMediaTypeIndex
            put 0 into tInputMediaTypeIndex

            local tAvailableInputMediaType
            local tCurrentMediaType
            repeat
               add 1 to tInputMediaTypeIndex
               put mediaFoundationVideoProcessorGetInputAvailableMediaType(tVideoProcessor, tInputMediaTypeIndex) \
                     into tAvailableInputMediaType
               if tAvailableInputMediaType is 0 then
                  exit repeat
               end if

               local tResult
               mediaFoundationReaderStreamSetCurrentMediaType pReader1FilePath, tIndex, tAvailableInputMediaType
               put the result into tResult
               if tResult then
                  mediaFoundationReaderStreamSetCurrentMediaType pReader2FilePath, tIndex, tAvailableInputMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tAvailableInputMediaType
                     next repeat
                  end if
                  put mediaFoundationReaderStreamGetCurrentMediaType(pReader1FilePath, tIndex) into tCurrentMediaType
                  mediaFoundationVideoProcessorSetInputMediaType tVideoProcessor, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tAvailableInputMediaType
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if
                  mediaFoundationMediaTypeSetAttribute tCurrentMediaType, "video frame size", "400,300"

                  mediaFoundationVideoProcessorSetOutputMediaType tVideoProcessor, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tAvailableInputMediaType
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if

                  mediaFoundationWriterStreamSetInputMediaType pWriterFilePath, tIndex, tCurrentMediaType
                  put the result into tResult
                  if not tResult then
                     mediaFoundationDeleteMediaType tAvailableInputMediaType
                     mediaFoundationDeleteMediaType tCurrentMediaType
                     next repeat
                  end if

                  mediaFoundationDeleteMediaType tCurrentMediaType
                  put true into tFoundMatch

               end if

               mediaFoundationDeleteMediaType tAvailableInputMediaType
               if tFoundMatch then
                  exit repeat
               end if
            end repeat

            if not tFoundMatch then
               exit repeat
            end if
            break
         case "media type audio"
            mediaFoundationAddStreamToWriter pWriterFilePath, tAudioMediaType
            mediaFoundationDeleteMediaType tAudioMediaType

            put false into tFoundMatch
            repeat for each item tIntermediateFormat in \
                  "audio format Float,audio format PCM"
               mediaFoundationMediaTypeSetAttribute tPartialMediaType, "subtype", tIntermediateFormat
               mediaFoundationReaderStreamSetCurrentMediaType pReader1FilePath, tIndex, tPartialMediaType
               mediaFoundationReaderStreamSetCurrentMediaType pReader2FilePath, tIndex, tPartialMediaType
               if the result then
                  put mediaFoundationReaderStreamGetCurrentMediaType(pReader1FilePath, tIndex) into tCurrentMediaType
                  mediaFoundationWriterStreamSetInputMediaType pWriterFilePath, tIndex, tCurrentMediaType
                  put the result into tResult
                  mediaFoundationDeleteMediaType tCurrentMediaType
                  if tResult then
                     put true into tFoundMatch
                     exit repeat
                  end if
               end if
            end repeat
            if not tFoundMatch then
               exit repeat
            end if
            break
         default

      end switch

      mediaFoundationDeleteMediaType tPartialMediaType
      mediaFoundationDeleteMediaType tNativeMediaType
   end repeat

   local tDuration
   put mediaFoundationReaderDuration(pReader1FilePath) into tDuration

   if tFoundMatch then
      mediaFoundationWriterBeginWriting pWriterFilePath
      mediaFoundationTranscode pReader1FilePath, pWriterFilePath, tVideoProcessor, 0,0,0
      mediaFoundationTranscode pReader2FilePath, pWriterFilePath, tVideoProcessor, tDuration, 0, 0
      mediaFoundationWriterFinalizeWriting pWriterFilePath
   end if

   mediaFoundationDeleteReader pReader1FilePath
   mediaFoundationDeleteReader pReader2FilePath
   mediaFoundationDeleteWriter pWriterFilePath
   mediaFoundationDeleteVideoProcessor tVideoProcessor
end Concatenate
RelatedCommand: mediaFoundationCreateWriter, mediaFoundationCreateReader, mediaFoundationCreateVideoProcessor
Description

The reader and writer must already be created with mediaFoundationCreateReader and mediaFoundationCreateWriter. If a video stream is to be transcoded then the video processor must already be created.

While transcoding the mediaFoundationTranscode command will wait with messages and a progress message mediaFoundationTranscoding will be sent to the caller with a single parameter of the timestamp of the current sample being processed.