Excerpt |
---|
In this example, the code reads a |
Below is an example to put SCTE-104 Ad Marker in the m3u8
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;
}
|
Below is an example to put SCTE-35 Ad Marker in the .m3u8 file
Code Block |
---|
#include <iostream> #include <fstream> #include <vector> #include <string> const std::string input = "input.m3u8"; const std::string output = "output.m3u8"; const std::string ad_marker = "#EXT-X-SCTE35:<SCTE-35 AD MARKER>\n"; std::vector<std::string> read_playlist(const std::string& filename) { std::vector<std::string> playlist; std::ifstream file(filename); std::string line; while (std::getline(file, line)) { playlist.push_back(line); } return playlist; } int main() { // Read playlist auto playlist = read_playlist(input); // Create output playlist std::ofstream stream(output); // Write header stream << "#EXTM3U\n"; // Write segments for (const auto& segment : playlist) { stream << segment << "\n"; stream << ad_marker; } return 0; } |
The above code reads an input .m3u8
playlist file into memory, and then generates an output playlist file. After writing the header, the code iterates over the segments in the input playlist, writing each segment to the output playlist, followed by the SCTE-35 ad marker. The SCTE-35 ad marker in this example is a hard-coded string, but it can be replaced with a dynamic value based on your specific use case.