YouTube content creation is complex and rarely ends with filming and editing the video. After uploading, you’re typically encouraged to upload timestamp markers for essential parts of your video. In this post, I’ll show you a quick C# script that can help you generate links to specific sections of your videos so you can use them in promotional blog posts and social media posts.

YouTube Videos and Timestamps

You typically get a video identifier in alphanumeric characters when you upload a video. For example, my latest YouTube video has the identifier u5y_aUP5Td0. Additionally, as the webinar progresses, there are moments you’d like to highlight. YouTube will automatically link these timestamps when placed in the video’s description. Here’s an example.

00:00:00 Introduction 
00:01:30 Luce Carter 
00:02:55 Luce’s History with Databases 
00:08:10 Tables vs. Documents 
00:16:48 Term Mapping Summary 
00:22:26 MongoDB Atlas 
00:27:49 Browsing Documents 
00:38:25 .NET with MongoDB 
00:48:45 MongoDB University
00:50:05 MongoDB Realm (Mobile) 
00:53:17 MongoDB in Action 
01:01:43 Questions and Answers 
01:10:01 Outro

When viewed on YouTube, the timestamp values will hyperlink to specific video portions. That’s great, but this format is limited to YouTube and can be difficult to share on your blog or other mediums.

Wouldn’t it be cool to use C# 11 features to generate these links quickly? With the flexibility of my script, I can produce any output that works for me: HTML, markdown, CSV, etc.

Let’s look at the script, and we’ll break down the C# 11 features used to generate the output.


using System.Text.RegularExpressions;

var videoId = "u5y_aUP5Td0";

var timestamps = """
00:00:00 Introduction 
00:01:30 Luce Carter 
00:02:55 Luce’s History with Databases 
00:08:10 Tables vs. Documents 
00:16:48 Term Mapping Summary 
00:22:26 MongoDB Atlas 
00:27:49 Browsing Documents 
00:38:25 .NET with MongoDB 
00:48:45 MongoDB University
00:50:05 MongoDB Realm (Mobile) 
00:53:17 MongoDB in Action 
01:01:43 Questions and Answers 
01:10:01 Outro
""";

var results = timestamps
    .Split(Environment.NewLine)
    .Select((line, _) => Entry.Parse(line, videoId))
    .ToList();

foreach (var (entry, i) in results.Select((e, i) => (e, i)))
{
    Console.WriteLine($"#{i:00} - {entry.Time:g} - {entry.Description} - {entry.Url}");
}

public record Entry(TimeSpan Time, string Url, string Description)
{
    private static readonly Regex TimestampRegex =
        new("((?<hours>[0-9]{0,2}):)?(?<minutes>[0-9]{0,2}):(?<seconds>[0-9]{0,2})",
            RegexOptions.Compiled | RegexOptions.Singleline);
    
    public static Entry Parse(string line, string videoId)
    { 
        var format = $"https://www.youtube.com/watch?v={videoId}&t={{0}}s";
        
        var match = TimestampRegex.Match(line);
        var timeSpan = new TimeSpan(
            int.Parse(match.Groups["hours"].Value),
            int.Parse(match.Groups["minutes"].Value),
            int.Parse(match.Groups["seconds"].Value)
        );

        return new Entry(
            Time: timeSpan, 
            Url: string.Format(format, timeSpan.TotalSeconds),
            Description: line.Substring(line.IndexOf(" ", StringComparison.Ordinal))
        );
    }
}

The first part of the script asks the developer for a videoId and the timestamps in a raw string literal. From there, we parse each row of the string literal and generate the links to each timestamp. Finally, in my case, I write out the output to the console.

#00 - 0:00:00 -  Introduction  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=0s
#01 - 0:01:30 -  Luce Carter  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=90s
#02 - 0:02:55 -  Luce’s History with Databases  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=175s
#03 - 0:08:10 -  Tables vs. Documents  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=490s
#04 - 0:16:48 -  Term Mapping Summary  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=1008s
#05 - 0:22:26 -  MongoDB Atlas  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=1346s
#06 - 0:27:49 -  Browsing Documents  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=1669s
#07 - 0:38:25 -  .NET with MongoDB  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=2305s
#08 - 0:48:45 -  MongoDB University - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=2925s
#09 - 0:50:05 -  MongoDB Realm (Mobile)  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=3005s
#10 - 0:53:17 -  MongoDB in Action  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=3197s
#11 - 1:01:43 -  Questions and Answers  - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=3703s
#12 - 1:10:01 -  Outro - https://www.youtube.com/watch?v=u5y_aUP5Td0&t=4201s

If you’re getting started creating YouTube content, you might find this code helpful for parsing timestamps and posting links to video segments on your social media platforms.

Enjoy!