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

Compare with Current View Page History

« Previous Version 5 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-105/35 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.

Below is an example to put SCTE-104 Ad Marker in the m3u8

#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

#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.



  • No labels