How to Create FFMPEG Chapters using C# .NET Core 5.0

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FfmpegChapterTool
{
    class Program
    {
        // Config
        static readonly TimeSpan _videoLength = new TimeSpan(0, 6, 0);
        static readonly string _example = @"
00:00:00 - Intro
00:02:30 - The Middle
00:04:45 - Outro
";

        static readonly Regex _regex = new Regex(@"((\d{2}):(\d{2}):(\d{2}))\s+-\s+(.*)", RegexOptions.Compiled);

        record ParsedLine(string Title, TimeSpan Timestamp, string RawTimeStampValue);

        static bool TryParseLine(string input, out ParsedLine result)
        {
            var match = _regex.Match(input);

            result = null;

            if (!match.Success || match.Groups.Count < 5)
            {
                return false;
            }

            var title = match.Groups[5].Value;
            var rawTimeStampValue = match.Groups[1].Value;

            if (!TimeSpan.TryParse(rawTimeStampValue, out TimeSpan parsedTimestamp))
            {
                return false;
            }

            result = new ParsedLine(title, parsedTimestamp, rawTimeStampValue);

            return true;
        }

        static IEnumerable<ParsedLine> ReadFile(string path)
        {
            var fileStream = File.OpenRead(path);

            return ReadStream(fileStream);
        }

        static IEnumerable<ParsedLine> ReadString(string raw, Encoding encoding)
        {
            var memoryStream = new MemoryStream(encoding.GetBytes(raw ?? string.Empty));

            return ReadStream(memoryStream);
        }

        static IEnumerable<ParsedLine> ReadStream(Stream stream)
        {
            using var streamReader = new StreamReader(stream);
            string line = string.Empty;

            while ((line = streamReader.ReadLine()) != null)
            {
                if (TryParseLine(line, out ParsedLine result))
                {
                    yield return result;
                }
            }
        }

        static string GetChapter(int chapterNum, ParsedLine parsedLine, ParsedLine ParsedLineNext) => $@"# Chapter {chapterNum} - {parsedLine.Title}
[CHAPTER]
TIMEBASE=1/1000
# Chapter {chapterNum} starts at {parsedLine.RawTimeStampValue}
START={parsedLine.Timestamp.TotalSeconds * 1000}
# Chapter {chapterNum} ends at {ParsedLineNext.RawTimeStampValue}
END={ParsedLineNext.Timestamp.TotalSeconds * 1000 - 1}
title={parsedLine.Title}
";

        static void Main(string[] args)
        {
            // var results = ReadFile("test.md").ToList();
            var results = ReadString(_example, Encoding.UTF8).ToList();            

            for (int i = 0; i < results.Count - 1; i++)
            {
                var line = GetChapter(i + 1, results[i], results[i + 1]);

                Console.WriteLine(line);
            }

            var finalLine = GetChapter(results.Count, results.Last(), new ParsedLine("End", _videoLength, _videoLength.ToString()));

            Console.WriteLine(finalLine);
        }
    }
}
;