SIGN UP MEMBER LOGIN:    
ARTICLE

Subtitles Algorithm

Posted by Peter Articles | C# Language September 06, 2006
This article explains a simple and useful algorithm that helps you to implement subtitles into your simple video player.
Reader Level:
Download Files:
 


Hello, here is the sample program, how easily you can implement subtitles into your simple movie player. This sample code works with the HashTable which contains "key" and "value".
As a Key I used my own class that contains "min" and "max" value, it means starting frame in and ending frame. The class "Key" also contains overriding of base method "bool Equals(object obj)". Equals method will tell the HashTable, that key can be in some range (example: 23-43-How are you?) so, if current position of film is frame 35, HashTable finds the text "How are you". It's quite good and simple solution if you're rewinding the film.

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

using System.IO;

namespace Subtitles

{

          class Program

          {

                   static void Main(string[] args)

                   {

                             Hashtable table = FileToString();

                             Console.WriteLine("OUTPUT:" + table[new Key(333)]); // there you enter current video frame(example: 333)

                   }

                   static Hashtable FileToString()

                   {

                             /*the file sub.txt is int this solution in this form: start_frame-end_frame-text 345-543-

                             How are you? */

                             Hashtable table = new Hashtable();

                             Stream stream = new FileStream("sub.txt",FileMode.Open);

                             StreamReader read = new StreamReader(stream);

                             while (true)

                             {

                                      string line = read.ReadLine();

                                      if (line == null)

                                                break;

                                     string[] field = line.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                                      Key key = new Key(Convert.ToInt32(field[0]),Convert.ToInt32(field[1]));

                                      table.Add(key,field[2]);

                             }

                             return table;

                   }

          }

          class Key

          {

                   int min;

                   int max;

                   public Key(int min, int max)

                   {

                             this.max = max;

                             this.min = min;

                   }

                   public Key(int pos)

                   {

                             this.max = pos;

                             this.min = pos;

                   }

                   public override bool Equals(object obj)

                   {

                             Key pom = (Key)obj;

                             if (pom.max <= max && pom.min >=min)

                             {

                                      return true;

                             }

                             else

                                      return false;

                   }

                   public override int GetHashCode()

                   {

                             return 1;

                   }

          }

}

Login to add your contents and source code to this article
share this article :
post comment
 

how am i going to use this code in my programme ?

Posted by oguz karademirci Jan 02, 2011

Hi, I just wonder if you had a label in front of the video-panel where the subtitle text should me written out? I have a mediaplayer me and my friend is developing. Everything is done besides two things, this subtitle to work and to make the screensaver not come up in the movie. Big thanks if you want to help me / Nick

Posted by Nick Willander Jan 31, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor