Exemplo n.º 1
0
        private void Parse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                generateButton.IsEnabled = false;

                currentSrt = XlsParser.Load(textBox.Text);

                generateButton.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur");
            }
        }
Exemplo n.º 2
0
        public static SrtModel Load(String path)
        {
            SrtModel mainModel = new SrtModel();

            mainModel.Languages = new List <string>();

            mainModel.Path     = System.IO.Path.GetDirectoryName(path);
            mainModel.Filename = System.IO.Path.GetFileNameWithoutExtension(path);

            List <SrtLineModel> models = new List <SrtLineModel>();

            mainModel.Languages.Add("und");

            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(file))
                {
                    SrtLineModel current_model = null;

                    String lineString = null;

                    while ((lineString = reader.ReadLine()) != null)
                    {
                        if (lineString == "")
                        {
                            continue;
                        }

                        if (current_model == null)
                        {
                            current_model = new SrtLineModel();
                            current_model.Translations = new Dictionary <string, string>();
                        }

                        if (current_model.Line == 0)
                        {
                            current_model.Line = int.Parse(lineString);
                        }

                        else if (current_model.StartDate == null)
                        {
                            String[] date_array = lineString.Replace(" --> ", "-").Split('-');

                            String debut = date_array[0];
                            String fin   = date_array[1];

                            try
                            {
                                current_model.StartDate = DateTime.ParseExact(debut, "hh:mm:ss,fff", CultureInfo.InvariantCulture);
                            }
                            catch
                            {
                                throw new Exception("Ligne n°" + (current_model.Line) + ": timecode de debut [" + debut + "] non valide, le format doit être hh:mm:ss,fff");
                            }

                            try
                            {
                                current_model.EndDate = DateTime.ParseExact(fin, "hh:mm:ss,fff", CultureInfo.InvariantCulture);
                            }
                            catch
                            {
                                throw new Exception("Ligne n°" + (current_model.Line) + ": timecode de fin [" + fin + "] non valide, le format doit être hh:mm:ss,fff");
                            }
                        }
                        else
                        {
                            current_model.Translations["und"] = lineString;

                            models.Add(current_model);

                            current_model = null;
                        }
                    }
                }
            }


            mainModel.SrtLines = models;

            return(mainModel);
        }
Exemplo n.º 3
0
        //Read the contents of the file to the mySrtModelList list
        public static List <SrtModel> ParseSrt(string srtPath)
        {
            mySrtModelList = new List <SrtModel>();
            string line;

            using (FileStream fs = new FileStream(srtPath, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    StringBuilder sb = new StringBuilder();
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!line.Equals(""))
                        {
                            sb.Append(line).Append("@");
                            continue;
                        }

                        string[] parseStrs = sb.ToString().Split('@');
                        if (parseStrs.Length < 3)
                        {
                            sb.Remove(0, sb.Length);// Clear, otherwise it will affect the analysis of the next subtitle element</i>
                            continue;
                        }

                        SrtModel srt          = new SrtModel();
                        string   strToTime    = parseStrs[1];
                        int      beginHour    = int.Parse(strToTime.Substring(0, 2));
                        int      beginMintue  = int.Parse(strToTime.Substring(3, 2));
                        int      beginSecond  = int.Parse(strToTime.Substring(6, 2));
                        int      beginMSecond = int.Parse(strToTime.Substring(9, 3));
                        int      beginTime    = (beginHour * 3600 + beginMintue * 60 + beginSecond) * 1000 + beginMSecond;

                        int endHour    = int.Parse(strToTime.Substring(17, 2));
                        int endMintue  = int.Parse(strToTime.Substring(20, 2));
                        int endSecond  = int.Parse(strToTime.Substring(23, 2));
                        int endMSecond = int.Parse(strToTime.Substring(26, 2));
                        int endTime    = (endHour * 3600 + endMintue * 60 + endSecond) * 1000 + endMSecond;

                        srt.BeginHour    = beginHour;
                        srt.BeginMintue  = beginMintue;
                        srt.BeginSecond  = beginSecond;
                        srt.BeginMSecond = beginMSecond;
                        srt.BeginTime    = beginTime;

                        srt.EndHour    = endHour;
                        srt.EndMintue  = endMintue;
                        srt.EndSecond  = endSecond;
                        srt.EndMSecond = endMSecond;
                        srt.EndTime    = endTime;
                        string strBody = null;
                        for (int i = 2; i < parseStrs.Length; i++)
                        {
                            strBody += parseStrs[i];
                        }
                        srt.SrtString = strBody;
                        mySrtModelList.Add(srt);
                        sb.Remove(0, sb.Length);
                    }
                }
            }
            return(mySrtModelList);
        }
Exemplo n.º 4
0
        public static SrtModel Load(String path)
        {
            SrtModel mainModel = new SrtModel();

            mainModel.Languages = new List <string>();

            XSSFWorkbook hssfwb;

            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                hssfwb = new XSSFWorkbook(file);
            }

            mainModel.Path     = System.IO.Path.GetDirectoryName(path);
            mainModel.Filename = System.IO.Path.GetFileNameWithoutExtension(path);

            ISheet sheet = hssfwb.GetSheetAt(0);

            List <SrtLineModel> models = new List <SrtLineModel>();


            var first_row = sheet.GetRow(0);

            for (int colIndex = 2; colIndex < first_row.Cells.Count; colIndex++)
            {
                mainModel.Languages.Add(first_row.GetCell(colIndex).StringCellValue.Trim());
            }


            for (int row = 1; row <= sheet.LastRowNum; row++)
            {
                var currentRow = sheet.GetRow(row);

                if (currentRow.Cells.Count == 0)
                {
                    break;
                }

                var debut = currentRow.GetCell(0);


                if (String.IsNullOrEmpty(debut.StringCellValue))
                {
                    break;
                }

                if (debut.StringCellValue.Trim() == String.Empty)
                {
                    break;
                }


                var fin = currentRow.GetCell(1);

                var language_1_content = currentRow.GetCell(2);

                if (debut == null || fin == null || language_1_content == null)
                {
                    throw new Exception("Ligne n°" + (row + 1) + ": debut ==null ou fin == null ou language_1_content == null");
                }

                DateTime debutTime = DateTime.Now;

                try
                {
                    debutTime = DateTime.ParseExact(debut.StringCellValue.Trim(), "hh:mm:ss,fff", CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new Exception("Ligne n°" + (row + 1) + ": timecode de debut [" + debut.StringCellValue + "] non valide, le format doit être hh:mm:ss,fff");
                }

                DateTime finTime = DateTime.Now;

                try
                {
                    finTime = DateTime.ParseExact(fin.StringCellValue.Trim(), "hh:mm:ss,fff", CultureInfo.InvariantCulture);
                }
                catch
                {
                    throw new Exception("Ligne n°" + (row + 1) + ": timecode de fin [" + fin.StringCellValue + "] non valide, le format doit être hh:mm:ss,fff");
                }


                Dictionary <String, String> translations = new Dictionary <string, string>();

                for (int colIndex = 2; colIndex < currentRow.Cells.Count; colIndex++)
                {
                    String current_language = mainModel.Languages[colIndex - 2];
                    translations[current_language] = currentRow.GetCell(colIndex).StringCellValue.Trim();
                }


                SrtLineModel model = new SrtLineModel
                {
                    Line         = row,
                    StartDate    = debutTime,
                    EndDate      = finTime,
                    Translations = translations,
                };

                models.Add(model);
            }

            mainModel.SrtLines = models;

            return(mainModel);
        }