-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4frag_main.cc
173 lines (158 loc) · 6.75 KB
/
mp4frag_main.cc
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
/*
* This is port of OpenHttpStreamer for win32
* copyright (c) 2011 [email protected]
*
*
* Originally:
* copyright (c) 2010 ZAO Inventos (inventos.ru)
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
*
* This file is part of mp4frag.
*
* mp4grag is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mp4frag is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "mp4frag.hh"
#include <boost/program_options.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/error_code.hpp>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <stdexcept>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>
using namespace boost::system;
namespace bfs = boost::filesystem3;
using boost::lexical_cast;
namespace {
bfs::path manifest_name = "manifest.f4m";
std::string video_id("some_video");
std::vector<bfs::path> srcfiles;
bool produce_template = false;
bool manifest_only = false;
int fragment_duration;
}
void parse_options(int argc, char **argv) {
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("src", po::value< std::vector<bfs::path> >(&srcfiles), "source mp4 file name")
("video_id", po::value<std::string>(&video_id)->default_value("some_video"), "video id for manifest file")
("manifest", po::value<bfs::path>(&manifest_name)->default_value("manifest.f4m"), "manifest file name")
("fragmentduration", po::value<int>(&fragment_duration)->default_value(3000), "single fragment duration, ms")
("index", "make index files instead of full fragments")
("nofragments", "make manifest only")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help") || argc == 1 || srcfiles.size() == 0) {
std::cerr << desc << "\n";
exit(1);
}
produce_template = vm.count("index") != 0;
manifest_only = vm.count("nofragments") != 0;
}
boost::shared_ptr<Mapping> create_mapping(const std::string& filename) {
return boost::shared_ptr<Mapping>(new Mapping(filename.c_str()));
}
int main(int argc, char **argv) try {
parse_options(argc, argv);
std::vector<boost::shared_ptr<Media> > fileinfo_list;
BOOST_FOREACH(bfs::path& srcfile, srcfiles) {
boost::shared_ptr<Media> pmedia = make_fragments(srcfile.string(), fragment_duration);
fileinfo_list.push_back(pmedia);
pmedia->medianame = bfs::complete(srcfile).string();
}
if ( !manifest_only ) {
if ( produce_template ) {
std::filebuf out;
std::string indexname = (manifest_name.parent_path() / "index").string();
if ( out.open(indexname.c_str(), std::ios::out | std::ios::binary | std::ios::trunc) ) {
serialize(&out, fileinfo_list);
if ( !out.close() ) {
throw std::runtime_error("Error closing " + indexname);
}
}
else {
throw std::runtime_error("Error opening " + indexname);
}
#if TESTING
Mapping index(indexname.c_str());
for ( unsigned ix = 0; ix < fileinfo_list.size(); ++ix ) {
boost::shared_ptr<Media>& pmedia = fileinfo_list[ix];
bfs::path mediadir = manifest_name.parent_path() / lexical_cast<std::string>(ix);
if ( mkdir(mediadir.string().c_str(), 0755) == -1 && errno != EEXIST ) {
throw system_error(errno, get_system_category(), "mkdir " + mediadir.string());
}
for ( unsigned fragment = 1; fragment <= pmedia->fragments.size(); ++fragment ) {
std::filebuf out;
std::string fragment_basename = std::string("Seg1-Frag") + boost::lexical_cast<std::string>(fragment);
bfs::path fragment_file = mediadir / fragment_basename;
if ( out.open(fragment_file.string().c_str(), std::ios::out | std::ios::binary | std::ios::trunc) ) {
get_fragment(&out, ix, fragment - 1, index.data(), index.size(), create_mapping);
if ( !out.close() ) {
throw std::runtime_error("Error closing " + fragment_file.string());
}
}
else {
throw std::runtime_error("Error opening " + fragment_file.string());
}
}
}
#endif
}
else {
for ( unsigned ix = 0; ix < fileinfo_list.size(); ++ix ) {
boost::shared_ptr<Media>& pmedia = fileinfo_list[ix];
bfs::path mediadir = manifest_name.parent_path() / lexical_cast<std::string>(ix);
if ( _mkdir(mediadir.string().c_str()) == -1 && errno != EEXIST ) {
throw system_error(errno, get_system_category(), "mkdir " + mediadir.string());
}
for ( unsigned fragment = 1; fragment <= pmedia->fragments.size(); ++fragment ) {
std::filebuf out;
std::string fragment_basename = std::string("Seg1-Frag") + boost::lexical_cast<std::string>(fragment);
bfs::path fragment_file = mediadir / fragment_basename;
if ( out.open(fragment_file.string().c_str(), std::ios::out | std::ios::binary | std::ios::trunc) ) {
serialize_fragment(&out, pmedia, fragment - 1);
if ( !out.close() ) {
throw std::runtime_error("Error closing " + fragment_file.string());
}
}
else {
throw std::runtime_error("Error opening " + fragment_file.string());
}
}
}
}
}
std::filebuf manifest_filebuf;
if ( manifest_filebuf.open(manifest_name.string().c_str(),
std::ios::out | std::ios::binary | std::ios::trunc) ) {
get_manifest(&manifest_filebuf, fileinfo_list, video_id);
if ( !manifest_filebuf.close() ) {
std::stringstream errmsg;
errmsg << "Error closing " << manifest_name;
throw std::runtime_error(errmsg.str());
}
}
else {
throw std::runtime_error("Error opening " + manifest_name.string());
}
}
catch ( std::exception& e ) {
std::cerr << e.what() << "\n";
}