skip to content
Stylized letter A in teal with pink sparkle accents Khalid Abuhakmeh

YouTube Timestamp Links: Create Them by Hand or with C#

/ 4 min read

Updated:

A YouTube timestamp link is just the video URL with a t parameter added: https://www.youtube.com/watch?v=VIDEO_ID&t=90s jumps straight to the 90-second mark. You can also write it as &t=1m30s, drop the s and use a bare number like ?t=90, or use the short domain: https://youtu.be/VIDEO_ID?t=90. All of them work.

That’s handy for a single link, but YouTube content creation rarely stops at one timestamp. After uploading a video, you’re typically encouraged to add a full list of timestamp markers for the description. In this post, I’ll show you a quick C# script that takes a list of timestamps and generates links to each section, so you can reuse them in blog posts and social media posts instead of retyping URLs by hand.

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!

Comments

Related Posts