source: trunk/Utilities/MediaFoundation/SinkWriterSetInputMediaType02/Application.cpp @ 708

Last change on this file since 708 was 708, checked in by roman, 7 years ago
File size: 2.2 KB
Line 
1#include "stdafx.h"
2
3#include <codecapi.h>
4#include <mfapi.h>
5#include <mfidl.h>
6#include <mfreadwrite.h>
7
8#pragma comment(lib, "mfuuid.lib")
9#pragma comment(lib, "mfplat.lib")
10#pragma comment(lib, "mfreadwrite.lib")
11
12#define __C ATLENSURE_SUCCEEDED
13
14int _tmain(int argc, _TCHAR* argv[])
15{
16        __C(CoInitialize(NULL));
17    __C(MFStartup(MF_VERSION, MFSTARTUP_FULL));
18        {
19                // NOTE: Tutorial: Using the Sink Writer to Encode Video https://msdn.microsoft.com/en-us/library/windows/desktop/ff819477
20
21                LPCWSTR pszPath = L"D:\\Temp.mp4";
22
23                // NOTE: MFCreateSinkWriterFromURL function https://msdn.microsoft.com/en-us/library/windows/desktop/dd388105
24                CComPtr<IMFSinkWriter> pSinkWriter;
25                __C(MFCreateSinkWriterFromURL(pszPath, NULL, NULL, &pSinkWriter));
26
27                // NOTE: http://stackoverflow.com/questions/41447563/media-foundation-mp4-encoding-imfsinkwriter-doesnt-accept-pcm-input
28
29                CComPtr<IMFMediaType> pMediaTypeOut;
30                __C(MFCreateMediaType(&pMediaTypeOut));
31                __C(pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio)); 
32                __C(pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_AAC));
33                __C(pMediaTypeOut->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2));
34                __C(pMediaTypeOut->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100));
35                __C(pMediaTypeOut->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16));
36                //__C(pMediaTypeOut->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, 0));
37
38                DWORD streamIndex; 
39                __C(pSinkWriter->AddStream(pMediaTypeOut, &streamIndex));
40
41                CComPtr<IMFMediaType> pMediaTypeIn;
42                __C(MFCreateMediaType(&pMediaTypeIn)); 
43
44                __C(pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));   
45                __C(pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM));
46
47                __C(pMediaTypeIn->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2));
48                __C(pMediaTypeIn->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100));
49                //__C(pMediaTypeIn->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, 4));
50                //__C(pMediaTypeIn->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, 44100 * 4));
51                __C(pMediaTypeIn->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16));
52                __C(pMediaTypeIn->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, 1));
53
54                __C(pSinkWriter->SetInputMediaType(streamIndex, pMediaTypeIn, NULL));   
55                __C(pSinkWriter->BeginWriting());
56        }
57        return 0;
58}
59
Note: See TracBrowser for help on using the repository browser.