You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

In this example, the code reads a .m3u8 playlist file, extracts the times of the segments, and sorts them into an array of AdMarker structures. The code then scans the playlist, and whenever the time of the next ad marker is less than the time of the current segment, an SCTE-104 ad marker #EXT-X-CUE-OUT is inserted into the output file, with a duration of 30 seconds and a URI pointing to the next segment.

#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;
}



  • No labels