Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys
Here is a list with 3 fields:
StartTime , EndTime , SubtitleText

There is almost 1000 numbers in the list.

So I have a VideoElement which plays the video, and I made a TextBlock on it to show this list as its subtitle.

Could you please help me and let me know how may I do this?
I mean when it plays, the subtitle from the list, shows sync with video in the TextBlock?
Thanks.

What I have tried:

Actually I do not have any Idea.
The only idea I think is that maybe I should make a timeline line Adobe Premiere, and doing keyframe in the list, then link that to position of video.... I dunno.
Posted
Updated 8-Mar-24 21:40pm

That's complicated, because the user can pause, fast forward, or rewind at any time, and you need to keep the subtitles in sync with the video or it will just get very confusing.

Probably your best bet is to use the industry standard .SRT file - this may help: https://www.3playmedia.com/blog/create-srt-file/[^] - and you will find that most (if not all) video players will use .SRT files and automatically sync them with the video content.
 
Share this answer
 
Comments
Sh.H. 9-Mar-24 4:15am    
@OriginalGriff
Thanks for answer. Actually video players import srt into memory first, then from memory, play sync with video. Aren't they?
OriginalGriff 9-Mar-24 6:14am    
Probably: that's the way I'd do it, and SRT files are trivial compared with the video file size!
Another solution will be to use the 'DispatcherTimer' control to sync your titles -
C# DispatcherTimer properties[^]

Your code will look like the following, adjust the 'SubtitleRecord' class and initialization parts based on your actual data in your list -
C#
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace SubtitleSyncApp
{
    public partial class MainWindow : Window
    {
        private List<SubtitleRecord> subtitleList = new List<SubtitleRecord>();
        private DispatcherTimer subtitleTimer = new DispatcherTimer();
        private int currentIndex = 0;

        public MainWindow()
        {
            InitializeComponent();
            InitializeSubtitleList(); 
            //Populate your subtitleList with 1000 records as per the class below...

            videoPlayer.MediaOpened += VideoPlayer_MediaOpened;

            subtitleTimer.Interval = TimeSpan.FromMilliseconds(100); 
            //Adjust the interval as needed...
            subtitleTimer.Tick += SubtitleTimer_Tick;
        }

        private void VideoPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            subtitleTimer.Start();
        }

        private void SubtitleTimer_Tick(object sender, EventArgs e)
        {
            double currentPosition = videoPlayer.Position.TotalSeconds;

            //Now find the index of the subtitle that matches your player's current position...
            while (currentIndex < subtitleList.Count - 1 && currentPosition > subtitleList[currentIndex + 1].StartTime)
            {
                currentIndex++;
            }

            //Update your subtitle text accordingly...
            subtitleTextBlock.Text = subtitleList[currentIndex].SubtitleText;
        }

        private void InitializeSubtitleList()
        {
            //Populate your subtitleList with 1000 records / replace this with your actual data...
            for (int i = 0; i < 1000; i++)
            {
                subtitleList.Add(new SubtitleRecord
                {
                    StartTime = i * 5, 
                    //This is as an example, adjust start times as needed or per yur list...
                    EndTime = (i + 1) * 5,
                    SubtitleText = $"Subtitle {i + 1}"
                });
            }
        }
    }

    public class SubtitleRecord
    {
        public double StartTime { get; set; }
        public double EndTime { get; set; }
        public string SubtitleText { get; set; }
    }
}
 
Share this answer
 
Comments
Sh.H. 9-Mar-24 6:58am    
@Andre Oosthuizen
Thanks for codes.
1 - How may I use timecode like this : --> 01:23:04,567 ?
2 - If the user, drags the slider of video, the subtitles goes out of sync.
3 - The speed performance is not OK. Is it possible to enhance the performance? Like using thread? Because if we have for example 2000 subtitles, then searching 2000 items will takes time.
Andre Oosthuizen 9-Mar-24 7:16am    
Quote:How may I use timecode like this : --> 01:23:04,567 ?

You will not hard code the time, it is read from your list in the part -
double currentPosition = videoPlayer.Position.TotalSeconds;


Quote:If the user, drags the slider of video, the subtitles goes out of sync.

It should not as the same above code will read the position of the player and will show the title accordingly.

Quote:The speed performance is not OK. Is it possible to enhance the performance? Like using thread?

Correct, as I stated, this is only a pointer and you need to adjust it to work for you, not something I will be going into detail with, your experimental time is required.
Sh.H. 9-Mar-24 7:52am    
@Andre Oosthuizen
Thanks for reply.
Could you please let me know, what is your idea about the way that programs like SubtitleEdit do on sync playback subtitle? Just let me know what do you guess?
https://www.nikse.dk/subtitleedit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900