Excerpt |
---|
In this example, the code reads a |
Code Block |
---|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
const std::string input = "input.m3u8";
const std::string output = "output.m3u8";
struct AdMarker {
double time;
std::string url;
};
bool operator<(const AdMarker& a, const AdMarker& b) {
return a.time < b.time;
}
int main()
{
std::vector<std::string> playlist;
std::vector<AdMarker> markers;
// Read playlist
std::ifstream file(input);
std::string line;
while (std::getline(file, line))
{
playlist.push_back(line);
if (line[0] != '#')
{
// Extract time and add to markers
size_t pos = line.find(",");
double time = std::stod(line.substr(0, pos));
markers.push_back({time, line});
}
}
// Sort markers by time
std::sort(markers.begin(), markers.end());
// Insert ad markers
std::ofstream stream(output);
for (const auto& line : playlist)
{
stream << line << "\n";
if (line[0] != '#')
{
// Check if it's time to insert ad marker
size_t pos = line.find(",");
double time = std::stod(line.substr(0, pos));
while (markers.size() > 0 && markers.front().time < time)
{
stream << "#EXT-X-CUE-OUT:DURATION=30,URI=" << markers.front().url << "\n";
markers.erase(markers.begin());
}
}
}
return 0;
}
|
...