/// <summary> /// Saves the given MatroskaTagInfo into an XML file /// </summary> /// <param name="filename">Filename of the XML file</param> /// <param name="taginfo">the recording information the xml file should contain</param> public static void WriteTag(string filename, MatroskaTagInfo taginfo) { if (!Directory.Exists(Path.GetDirectoryName(filename))) { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } XmlDocument doc = new XmlDocument(); XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlNode tagsNode = doc.CreateElement("tags"); XmlNode tagNode = doc.CreateElement("tag"); tagNode.AppendChild(AddSimpleTag("TITLE", taginfo.title, doc)); tagNode.AppendChild(AddSimpleTag("COMMENT", taginfo.description, doc)); tagNode.AppendChild(AddSimpleTag("GENRE", taginfo.genre, doc)); tagNode.AppendChild(AddSimpleTag("CHANNEL_NAME", taginfo.channelName, doc)); tagNode.AppendChild(AddSimpleTag("EPISODENAME", taginfo.episodeName, doc)); tagNode.AppendChild(AddSimpleTag("SERIESNUM", taginfo.seriesNum, doc)); tagNode.AppendChild(AddSimpleTag("EPISODENUM", taginfo.episodeNum, doc)); tagNode.AppendChild(AddSimpleTag("EPISODEPART", taginfo.episodePart, doc)); tagNode.AppendChild(AddSimpleTag("STARTTIME", taginfo.startTime.ToString("yyyy-MM-dd HH:mm"), doc)); tagNode.AppendChild(AddSimpleTag("ENDTIME", taginfo.endTime.ToString("yyyy-MM-dd HH:mm"), doc)); tagsNode.AppendChild(tagNode); doc.AppendChild(tagsNode); doc.InsertBefore(xmldecl, tagsNode); doc.Save(filename); }
private static Dictionary <string, MatroskaTagInfo> GetTagInfoForDirectory(string aDirectory) { Dictionary <string, MatroskaTagInfo> foundTagInfo = new Dictionary <string, MatroskaTagInfo>(); try { string[] importDirs = new string[] {}; // get all subdirectories try { importDirs = Directory.GetDirectories(aDirectory, "*", SearchOption.TopDirectoryOnly); } catch (Exception ex) { Log.Info("Error while reading subdirectories of {0}: {1}", aDirectory, ex); } List <string> searchDirs = new List <string>(importDirs); foreach (string subDir in searchDirs) { Dictionary <string, MatroskaTagInfo> foundTagsInSubDir = GetTagInfoForDirectory(subDir); foreach (KeyValuePair <string, MatroskaTagInfo> kvp in foundTagsInSubDir) { foundTagInfo.Add(kvp.Key, kvp.Value); } } try { string[] importFiles = Directory.GetFiles(aDirectory, "*.xml", SearchOption.TopDirectoryOnly); foreach (string xmlFile in importFiles) { try { MatroskaTagInfo importTag = ReadTag(xmlFile); foundTagInfo[xmlFile] = importTag; } catch (Exception ex) { Log.Info("Error while reading matroska informations in file {0}: {1}", xmlFile, ex); } } } catch (Exception ex) { Log.Info("Error while reading matroska informations in directory {0}: {1}", aDirectory, ex); } } catch (Exception ex) { Log.Info("Error while reading all matroska informations : ", ex); } return(foundTagInfo); }
/// <summary> /// Reads Matroska tag files /// </summary> /// <param name="filename">Path to an XML file containing recording infos</param> /// <returns>The Matroska tag object</returns> public static MatroskaTagInfo ReadTag(string filename) { if (!File.Exists(filename)) { return(null); } MatroskaTagInfo info = new MatroskaTagInfo(); XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList simpleTags = doc.SelectNodes("/tags/tag/SimpleTag"); if (simpleTags != null) { foreach (XmlNode simpleTag in simpleTags) { string tagName = simpleTag.ChildNodes[0].InnerText; switch (tagName) { case "TITLE": info.title = simpleTag.ChildNodes[1].InnerText; break; case "COMMENT": info.description = simpleTag.ChildNodes[1].InnerText; break; case "GENRE": info.genre = simpleTag.ChildNodes[1].InnerText; break; case "CHANNEL_NAME": info.channelName = simpleTag.ChildNodes[1].InnerText; break; case "SERIESNUM": info.seriesNum = simpleTag.ChildNodes[1].InnerText; break; case "EPISODENUM": info.episodeNum = simpleTag.ChildNodes[1].InnerText; break; case "EPISODEPART": info.episodePart = simpleTag.ChildNodes[1].InnerText; break; case "EPISODENAME": info.episodeName = simpleTag.ChildNodes[1].InnerText; break; case "STARTTIME": try { info.startTime = DateTime.ParseExact(simpleTag.ChildNodes[1].InnerText, "yyyy-MM-dd HH:mm", null); } catch (Exception) { info.startTime = SqlDateTime.MinValue.Value; } break; case "ENDTIME": try { info.endTime = DateTime.ParseExact(simpleTag.ChildNodes[1].InnerText, "yyyy-MM-dd HH:mm", null); } catch (Exception) { info.endTime = SqlDateTime.MinValue.Value; } break; } } } return(info); }
private void TsCopier(object itemlist, Recording rec, Schedule newSchedule) { string[] bufferListObject; bufferListObject = new string[3]; List<string[]> _itemlist = (List<string[]>)itemlist; bool foundHeader = false; bufferListObject = _itemlist[0]; string targetTs = Path.GetDirectoryName(bufferListObject[2]) + "\\" + Path.GetFileNameWithoutExtension(bufferListObject[2]) + "_buffer.ts"; try { Log.Info("TsCopier: targetTs {0}", targetTs); using (FileStream writer = new FileStream(targetTs, FileMode.CreateNew, FileAccess.Write)) { for (int i = 0; i < _itemlist.Count; i++) { bufferListObject = _itemlist[i]; try { if (File.Exists(bufferListObject[0])) { using (FileStream reader = new FileStream(bufferListObject[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { Log.Info("TsCopier: TSfilename {0}", bufferListObject[0]); Log.Debug("TsCopier: TSfilename filesize {0}", bufferListObject[1]); if (!foundHeader) { byte[] prebuf = new byte[1024 * 1024]; int bytesPreRead; bytesPreRead = reader.Read(prebuf, 0, 1024 * 1024); long position = 0; // find TS packet header while (bytesPreRead > 0 && !foundHeader) { for (int x = 0; x < 1024 * 1024 - 376; x++) { if (prebuf[x] == 0x47 && prebuf[x + 188] == 0x47 && prebuf[x + 376] == 0x47) { Log.Debug("TsCopier: TS packet header found at {0} pos in {1}.", x, bufferListObject[0]); position = x; foundHeader = true; break; } } bytesPreRead = reader.Read(prebuf, 0, 1024 * 1024); } reader.Position = position; if (!foundHeader) { Log.Debug("TsCopier: TS packet header not found in {0}.", bufferListObject[0]); break; } } byte[] buf = new byte[1024 * 1024]; int bytesRead = reader.Read(buf, 0, 1024 * 1024); while (bytesRead > 0) { if (reader.Position > Convert.ToInt64(bufferListObject[1])) bytesRead -= (int)(reader.Position - Convert.ToInt64(bufferListObject[1])); if (bytesRead <= 0) break; writer.Write(buf, 0, bytesRead); bytesRead = reader.Read(buf, 0, 1024 * 1024); Thread.Sleep(100); } reader.Close(); } } } catch (Exception ex) { Log.Error("TsCopier exception: {0}", ex); } } writer.Flush(); writer.Close(); Log.Info("TsCopier: Done {0}", targetTs); } } catch (Exception ex) { Log.Error("TsCopier Exception: {0}", ex); } try { Log.Debug("TsCopier: Creating Recording entry for {0}", targetTs); RecordingDetail recDetail = new RecordingDetail(newSchedule, newSchedule.ReferencedChannel(), DateTime.Now, false); recDetail.Recording = new Recording(recDetail.Schedule.IdChannel, recDetail.Schedule.IdSchedule, false, rec.StartTime, DateTime.Now, rec.Title + " (from buffer)", recDetail.Program.Description, recDetail.Program.Genre, targetTs, recDetail.Schedule.KeepMethod, recDetail.Schedule.KeepDate, 0, rec.IdServer, recDetail.Program.EpisodeName, recDetail.Program.SeriesNum, recDetail.Program.EpisodeNum, recDetail.Program.EpisodePart); recDetail.Recording.Persist(); IUser user = recDetail.User; TsBufferExtractor.Controller.Fire(this, new TvServerEventArgs(TvServerEventType.RecordingEnded, new VirtualCard(user), (User)user, recDetail.Schedule, recDetail.Recording)); MatroskaTagInfo info = new MatroskaTagInfo(); info.title = rec.Title + " (from buffer)"; info.description = recDetail.Program.Description; info.genre = recDetail.Program.Genre; info.channelName = recDetail.Schedule.ReferencedChannel().DisplayName; info.episodeName = recDetail.Program.EpisodeName; info.seriesNum = recDetail.Program.SeriesNum; info.episodeNum = recDetail.Program.EpisodeNum; info.episodePart = recDetail.Program.EpisodePart; info.startTime = rec.StartTime; info.endTime = DateTime.Now; MatroskaTagHandler.WriteTag(System.IO.Path.ChangeExtension(targetTs, ".xml"), info); Log.Info("TsCopier: Finished the job."); } catch (Exception ex) { Log.Error("TsCopier Exception: {0}", ex); } }
private Recording BuildRecordingFromTag(string aFileName, MatroskaTagInfo aTag) { Recording tagRec = null; try { string physicalFile = GetRecordingFilename(aFileName); tagRec = new Recording(GetChannelIdByDisplayName(aTag.channelName), GetRecordingStartTime(physicalFile), GetRecordingEndTime(physicalFile), aTag.title, aTag.description, aTag.genre, physicalFile, 0, SqlDateTime.MaxValue.Value, 0, GetServerId() ); } catch (Exception ex) { MessageBox.Show(string.Format("Could not build recording from tag: {0}\n{1}", aFileName, ex.Message)); } return tagRec; }
/// <summary> /// Saves the given MatroskaTagInfo into an XML file /// </summary> /// <param name="filename">Filename of the XML file</param> /// <param name="taginfo">the recording information the xml file should contain</param> public static void WriteTag(string filename, MatroskaTagInfo taginfo) { if (!Directory.Exists(Path.GetDirectoryName(filename))) { Directory.CreateDirectory(Path.GetDirectoryName(filename)); } XmlDocument doc = new XmlDocument(); XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlNode tagsNode = doc.CreateElement("tags"); XmlNode tagNode = doc.CreateElement("tag"); tagNode.AppendChild(AddSimpleTag("TITLE", taginfo.title, doc)); tagNode.AppendChild(AddSimpleTag("COMMENT", taginfo.description, doc)); tagNode.AppendChild(AddSimpleTag("GENRE", taginfo.genre, doc)); tagNode.AppendChild(AddSimpleTag("CHANNEL_NAME", taginfo.channelName, doc)); tagNode.AppendChild(AddSimpleTag("EPISODENAME", taginfo.episodeName, doc)); tagNode.AppendChild(AddSimpleTag("SERIESNUM", taginfo.seriesNum, doc)); tagNode.AppendChild(AddSimpleTag("EPISODENUM", taginfo.episodeNum, doc)); tagNode.AppendChild(AddSimpleTag("EPISODEPART", taginfo.episodePart, doc)); tagNode.AppendChild(AddSimpleTag("STARTTIME", taginfo.startTime.ToString("yyyy-MM-dd HH:mm"), doc)); tagNode.AppendChild(AddSimpleTag("ENDTIME", taginfo.endTime.ToString("yyyy-MM-dd HH:mm"), doc)); tagsNode.AppendChild(tagNode); doc.AppendChild(tagsNode); doc.InsertBefore(xmldecl, tagsNode); doc.Save(filename); }
/// <summary> /// Reads Matroska tag files /// </summary> /// <param name="filename">Path to an XML file containing recording infos</param> /// <returns>The Matroska tag object</returns> public static MatroskaTagInfo ReadTag(string filename) { if (!File.Exists(filename)) { return null; } MatroskaTagInfo info = new MatroskaTagInfo(); XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList simpleTags = doc.SelectNodes("/tags/tag/SimpleTag"); if (simpleTags != null) { foreach (XmlNode simpleTag in simpleTags) { string tagName = simpleTag.ChildNodes[0].InnerText; switch (tagName) { case "TITLE": info.title = simpleTag.ChildNodes[1].InnerText; break; case "COMMENT": info.description = simpleTag.ChildNodes[1].InnerText; break; case "GENRE": info.genre = simpleTag.ChildNodes[1].InnerText; break; case "CHANNEL_NAME": info.channelName = simpleTag.ChildNodes[1].InnerText; break; case "SERIESNUM": info.seriesNum = simpleTag.ChildNodes[1].InnerText; break; case "EPISODENUM": info.episodeNum = simpleTag.ChildNodes[1].InnerText; break; case "EPISODEPART": info.episodePart = simpleTag.ChildNodes[1].InnerText; break; case "EPISODENAME": info.episodeName = simpleTag.ChildNodes[1].InnerText; break; case "STARTTIME": try { info.startTime = DateTime.ParseExact(simpleTag.ChildNodes[1].InnerText, "yyyy-MM-dd HH:mm", null); } catch (Exception) { info.startTime = SqlDateTime.MinValue.Value; } break; case "ENDTIME": try { info.endTime = DateTime.ParseExact(simpleTag.ChildNodes[1].InnerText, "yyyy-MM-dd HH:mm", null); } catch (Exception) { info.endTime = SqlDateTime.MinValue.Value; } break; } } } return info; }
private void WriteMatroskaFile(RecordingDetail recDetail) { if (_createTagInfoXML) { string fileName = recDetail.FileName; MatroskaTagInfo info = new MatroskaTagInfo(); info.title = recDetail.Program.Title; info.description = recDetail.Program.Description; info.genre = recDetail.Program.Genre; info.channelName = recDetail.Schedule.ReferencedChannel().DisplayName; info.episodeName = recDetail.Program.EpisodeName; info.seriesNum = recDetail.Program.SeriesNum; info.episodeNum = recDetail.Program.EpisodeNum; info.episodePart = recDetail.Program.EpisodePart; info.startTime = recDetail.RecordingStartDateTime; info.endTime = recDetail.EndTime; MatroskaTagHandler.WriteTag(System.IO.Path.ChangeExtension(fileName, ".xml"), info); } }