Teoman shipahi
Teoman shipahi

Reputation: 23052

What is the type of subtitle (.srt) time frame?

I am working on C# application which will read .srt file and show it on screen.

File format is simply like this;

1   00:00:06,000    00:00:07,400    Enjoy the movie! 
2   00:00:07,500    00:00:09,500    Hi, my name is Mary
3   00:00:22,000    00:00:25,000    Hello my name is John.

I am using a timer which is ticking every 100 milliseconds and on every single tick I want to check if value is between start and end. In this case first will be between 00:00:06,000 and 00:00:07,400. But I am not sure what value I should use. I tried to parse it to DateTime but it did not convert.

Is there any idea?

Upvotes: 0

Views: 2331

Answers (3)

Victor Chekalin
Victor Chekalin

Reputation: 776

Use TimeSpan. Here the simple Console Project to show subtitles on the correct time

    class Program
    {
    private static Queue<Subtitle> _subtitles;

    private static Subtitle _activeSubtitle;

    private static TimeSpan _currentTime = new TimeSpan();

    static void Main(string[] args)
    {
        _subtitles = new Queue<Subtitle>();

        Subtitle title1 = new Subtitle()
                              {
                                  StartTime = TimeSpan.Parse("00:00:06,000"),
                                  EndTime =  TimeSpan.Parse("00:00:07,400"),
                                  Text = "Enjoy the movie!"
                              };

        Subtitle title2 = new Subtitle()
                              {
                                  StartTime = TimeSpan.Parse("00:00:07,500"),
                                  EndTime =  TimeSpan.Parse("00:00:09,500"),
                                  Text = "Hi, my name is Mary"
                              };

        Subtitle title3 = new Subtitle()
                              {
                                  StartTime = TimeSpan.Parse("00:00:22,000"),
                                  EndTime =  TimeSpan.Parse("00:00:25,000"),
                                  Text = "Hello my name is John."
                              };

        _subtitles.Enqueue(title1);
        _subtitles.Enqueue(title2);
        _subtitles.Enqueue(title3);

        Timer timer = new Timer(ShowSubtitles, null, 0, 100);

        while (_currentTime <= new TimeSpan(0, 0, 0, 30))
        {

        }
        Console.WriteLine("End");
    }

    private static void ShowSubtitles(object state)
    {

        if (_activeSubtitle == null && _subtitles.Count > 0)
            _activeSubtitle = _subtitles.Dequeue();
        Console.WriteLine(_currentTime);

        if (_activeSubtitle != null)
        {


            if (_currentTime >= _activeSubtitle.StartTime && _currentTime <= _activeSubtitle.EndTime)
                Console.WriteLine("\t{0}", _activeSubtitle.Text);

            if (_currentTime >= _activeSubtitle.EndTime)
                _activeSubtitle = null;
        }
        _currentTime = _currentTime.Add(new TimeSpan(0, 0, 0, 0, 100));

    }
}

internal class Subtitle
{
    public TimeSpan StartTime { get; set; }
    public TimeSpan EndTime { get; set; }
    public string Text { get; set; }
}

Upvotes: 1

pabdulin
pabdulin

Reputation: 35229

You can use either DateTime or TimeSpan but you need to supply correct format to ParseExact function.

Upvotes: 1

Diego Garcia
Diego Garcia

Reputation: 1144

You have to parse using the type TimeSpan take a look here

Upvotes: 1

Related Questions