-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslideshow.cpp
More file actions
162 lines (118 loc) · 4.09 KB
/
Copy pathslideshow.cpp
File metadata and controls
162 lines (118 loc) · 4.09 KB
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
#include "stdafx.h"
#include "options.h"
#include "util.h"
using namespace std;
using namespace primo::avblocks;
using namespace primo::codecs;
void printError(const wchar_t* action, const primo::error::ErrorInfo* e)
{
if (action)
{
wcout << action << L": ";
}
if (primo::error::ErrorFacility::Success == e->facility())
{
wcout << L"Success" << endl;
return;
}
if (e->message())
{
wcout << e->message() << L", ";
}
wcout << L"facility:" << e->facility() << L", error:" << e->code() << endl;
}
MediaBuffer* createMediaBufferForFile(const wchar_t* filename)
{
HANDLE h = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE == h)
return NULL;
int32_t imgsize = GetFileSize(h, NULL);
MediaBuffer* mediaBuffer = Library::createMediaBuffer(imgsize);
DWORD bytesRead;
ReadFile(h, mediaBuffer->start(), imgsize, &bytesRead, NULL);
mediaBuffer->setData(0, imgsize);
CloseHandle(h);
return mediaBuffer;
}
bool slideshow(Options& opt)
{
const double inputFramerate = 25.0;
const int imageCount = 250;
bool_t res;
wstring imgdir( opt.input_dir);
wstring outFilename (opt.output_file);
DeleteFile(outFilename.c_str());
auto transcoder = primo::make_ref(Library::createTranscoder());
// Transcoder demo mode must be enabled,
// in order to use the production release for testing (without a valid license)
transcoder->setAllowDemoMode(true);
// Load image info
auto info = primo::make_ref(Library::createMediaInfo());
{
wstring firstImage(imgdir);
firstImage.append(L"\\cube0000.jpeg");
info->inputs()->at(0)->setFile(firstImage.c_str());
res = info->open();
printError(L"Load Info", info->error());
if (!res)
return false;
}
// Configure input
{
auto vinfo = primo::make_ref((VideoStreamInfo*) info->outputs()->at(0)->pins()->at(0)->streamInfo()->clone());
vinfo->setFrameRate(inputFramerate);
auto pin = primo::make_ref(Library::createMediaPin());
pin->setStreamInfo(vinfo.get());
auto socket = primo::make_ref(Library::createMediaSocket());
socket->pins()->add(pin.get());
transcoder->inputs()->add(socket.get());
}
// Configure output
{
auto socket = primo::make_ref(Library::createMediaSocket(opt.preset.name));
socket->setFile(outFilename.c_str());
transcoder->outputs()->add(socket.get());
}
res = transcoder->open();
printError(L"Open Transcoder", transcoder->error());
if (!res)
return false;
// Encode images
auto mediaSample = primo::make_ref(Library::createMediaSample());
for(int i = 0; i < imageCount; i++)
{
WCHAR imgfile[MAX_PATH];
wsprintf(imgfile, L"%s\\cube%04d.jpeg", imgdir.c_str(), i);
auto buffer = primo::make_ref(createMediaBufferForFile(imgfile));
mediaSample->setBuffer(buffer.get());
// the correct start time is required by the transcoder
mediaSample->setStartTime(i / inputFramerate);
res = transcoder->push(0, mediaSample.get());
if (!res)
{
printError(L"Push Transcoder", transcoder->error());
return false;
}
}
if(!transcoder->flush())
{
printError(L"Flush Transcoder", transcoder->error());
return false;
}
transcoder->close();
wcout << L"Output video: \"" << outFilename << L"\"" << endl;
return true;
}
int _tmain(int argc, wchar_t* argv[])
{
Options opt;
switch(prepareOptions( opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
}
primo::avblocks::Library::initialize();
bool slideshowResult = slideshow(opt);
primo::avblocks::Library::shutdown();
return slideshowResult ? 0 : 1;
}