public Frame(int w, int h, int t, byte[] data, int beg, int sz, int szCaption, DateTime date) { width = w; height = h; szData = sz; szPack = sz; type = t; time = date; if (beg >= 0) { this.data = new byte[sz]; Array.Copy(data, beg, this.data, 0, sz); caption = VideoHead.GetCaption(data, beg + sz, szCaption); } else { this.data = data; } }
static public string FindAnchor(string path, string anchor, out DateTime posTime, out string uid) { string ret = string.Empty; string[] ss = anchor.Split(' '); posTime = DateTime.Today; uid = string.Empty; if (ss.Length == 3) //должна быть строка в виде "уид 2011-10-18 16:31:54" { uid = ss[0].Trim(); string[] ss1 = ss[1].Trim().Split('-'); string[] ss2 = ss[2].Trim().Split(':'); if (ss1.Length == 3 && ss2.Length == 3) //формат даты и времени верный { int y = int.Parse(ss1[0]); //год int m = int.Parse(ss1[1]); //месяц int d = int.Parse(ss1[2]); //день int h = int.Parse(ss2[0]); //часы int mm = int.Parse(ss2[1]); //минуты int s = int.Parse(ss2[2]); //секунды DateTime dt = new DateTime(y, m, d, h, mm, s); string[] dirs = System.IO.Directory.GetDirectories(path, uid, System.IO.SearchOption.AllDirectories); for (int i = 0; i < dirs.Length && ret.Length == 0; i++) { string[] frm = System.IO.Directory.GetFiles(dirs[i], "*.frm", System.IO.SearchOption.AllDirectories); for (int j = 0; j < frm.Length; j++) { VideoHead video = new VideoHead(frm[j]); if (!video.IsBad) { if (video.DateBeg <= dt && dt <= video.DateEnd) { ret = frm[j]; posTime = dt; break; } } } } } } return(ret); }
VideoFile LoadVideoFile(string path) { try { VideoHead vh = new VideoHead(path); if (!vh.IsBad) { VideoFile vf = new VideoFile(); vf.fileName = path; string[] ss = path.Split(System.IO.Path.DirectorySeparatorChar); vf.uid = ss[ss.Length - 2]; //предпоследняя строка UID бота (папка) vf.head = vh; return(vf); } } catch (Exception e) { } return(null); }
public void Record() { int count = 0; int c_uid = 0; while (c_uid <= 0) { int count2 = sc.Receive(buffer, count, buffer.Length - count, SocketFlags.None); count += count2; c_uid = Array.IndexOf(buffer, (byte)0, 0, count); } if (c_uid > 0) { uid = Encoding.ASCII.GetString(buffer, 0, c_uid); string path = System.Windows.Forms.Application.StartupPath + "\\video\\" + uid; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } DateTime dateBeg = DateTime.Now; nameFile = path + '\\' + dateBeg.ToString("yyyyMMddHHmmss") + ".frm"; int beg = c_uid + 1; //System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\bot.txt"); System.IO.FileStream fs = new System.IO.FileStream(nameFile, System.IO.FileMode.CreateNew); fs.WriteByte((byte)'T'); fs.WriteByte((byte)'A'); fs.WriteByte((byte)'G'); byte[] m = VideoHead.DateTimeToBytes(dateBeg); fs.Write(m, 0, m.Length); //начало записи fs.Write(m, 0, m.Length); //конец записи fs.Write(BitConverter.GetBytes(((IPEndPoint)sc.RemoteEndPoint).Address.Address), 0, 4); while (true) { try { if (beg == 0) { count = sc.Receive(buffer, 0, buffer.Length, SocketFlags.None); } if (count == 0) { break; } fs.Write(buffer, beg, count - beg); beg = 0; //sw.Write(count); //sw.Write(' '); } catch { break; } } DateTime dateEnd = DateTime.Now; m = VideoHead.DateTimeToBytes(dateEnd); fs.Seek(10, System.IO.SeekOrigin.Begin); fs.Write(m, 0, m.Length); fs.Close(); sc.Close(); //sw.Close(); } Stop(true); }
public static Video LoadVideo(string nameFile) { byte[] data = System.IO.File.ReadAllBytes(nameFile); DateTime dateBeg = DateTime.Now, dateEnd = dateBeg; string ip = string.Empty; int i = 0; if (data[i] == 'T' && data[i + 1] == 'A' && data[i + 2] == 'G') { i += 3; dateBeg = VideoHead.GetDateTime(data, i); i += 7; dateEnd = VideoHead.GetDateTime(data, i); i += 7; ip = new System.Net.IPAddress(BitConverter.ToUInt32(data, i)).ToString(); i += 4; } if (data[i] == 'F' && data[i + 1] == 'R' && data[i + 2] == 'M') { i += 3; int sz = VideoHead.GetShort(data, i); i += 2; int freq = data[i]; i++; //частота кадров int szNameVideo = VideoHead.GetShort(data, i); i += 2; int szNameProcess = VideoHead.GetShort(data, i); i += 2; Video video = new Video(freq, 2, dateBeg, dateEnd, ip); video.NameVideo = Encoding.ASCII.GetString(data, i, szNameVideo); sz = i + szNameVideo + szNameProcess; int numFrame = 0; //номер кадра byte[] data2 = null; byte[] dataTmp = null; while (sz + 4 < data.Length && sz > 0) { int sz2 = BitConverter.ToInt32(data, sz); sz += 4; if (sz + sz2 > data.Length) { break; } Server.LZW lzw = new Server.LZW(); if (dataTmp == null || dataTmp.Length < sz2) { dataTmp = new byte[sz2]; } if (data2 == null || data2.Length < 20 * sz2) { data2 = new byte[20 * sz2]; } Array.Copy(data, sz, dataTmp, 0, sz2); int sz3 = lzw.from_lzw(dataTmp, data2); i = 0; while (i < sz3) { numFrame++; int szStruct = VideoHead.GetShort(data2, i); if (i + szStruct >= sz3 || szStruct < 0) { break; } int width = VideoHead.GetShort(data2, i + 2); int height = VideoHead.GetShort(data2, i + 4); int szData = VideoHead.GetInt(data2, i + 6); int type = VideoHead.GetShort(data2, i + 10); int szCaption = VideoHead.GetShort(data2, i + 12); DateTime timeFrame = dateBeg.AddSeconds(numFrame / freq); //предположительное время кадра if (szStruct > 14) //было добавлено время на стороне бота { timeFrame = VideoHead.GetDateTime(data2, i + 14); } if (sz3 >= i + szStruct + szData && width < 6000 && height < 3000) { video.AddFrame(width, height, type, data2, i + szStruct, szData, szCaption, timeFrame); } i += szStruct + szData + szCaption; // if (video.frames.Count == 168) // video.frames.Count.ToString(); } sz += sz2; } video.ArrangeCaptions(); video.CorrectTime(); return(video); } return(null); }
public VideoHead(string nameFile) { byte[] data = new byte[512]; System.IO.FileStream fs = new System.IO.FileStream(nameFile, System.IO.FileMode.Open, System.IO.FileAccess.Read); int rd = fs.Read(data, 0, 3); if (data[0] == 'T' && data[1] == 'A' && data[2] == 'G') { rd = fs.Read(data, 0, 18); if (rd == 18) { timeBeg = GetDateTime(data, 0); timeEnd = GetDateTime(data, 7); ip = new System.Net.IPAddress(BitConverter.ToUInt32(data, 14)).ToString(); } } else { fs.Seek(0, System.IO.SeekOrigin.Begin); timeBeg = DateTime.Now; timeEnd = DateTime.Now; ip = "nothing"; } rd = fs.Read(data, 0, 10); if (data[0] == 'F' && data[1] == 'R' && data[2] == 'M' && rd == 10) { int sz = GetShort(data, 3); freq = data[5]; int szNameVideo = GetShort(data, 6); int szNameProcess = GetShort(data, 8); sz += szNameVideo + szNameProcess; rd += fs.Read(data, rd, sz - rd); name = Encoding.ASCII.GetString(data, 10, szNameVideo); nameProcess = Encoding.ASCII.GetString(data, 10 + szNameVideo, szNameProcess); try { captions = new List <string>(); bool stop = false; byte[] data2 = null; while (fs.Read(data, 0, 4) == 4 && !stop) { sz = BitConverter.ToInt32(data, 0); if (data.Length < sz) { data = new byte[sz]; } if (fs.Read(data, 0, sz) != sz) { break; } Server.LZW lzw = new Server.LZW(); if (data2 == null || data2.Length < 20 * sz) { data2 = new byte[20 * sz]; } int sz2 = lzw.from_lzw(data, data2); int i = 0; while (i < sz2) { int szStruct = VideoHead.GetShort(data2, i); if (szStruct < 0) { stop = true; break; } int szData = VideoHead.GetInt(data2, i + 6); if (szData < 0) { stop = true; break; } int szCaption = VideoHead.GetShort(data2, i + 12); if (szCaption < 0) { stop = true; break; } i += szStruct + szData; if (szCaption > 0 && i <= sz2) { string s = GetCaption(data2, i, szCaption); if (!captions.Contains(s)) { captions.Add(s); } i += szCaption; } } } /* * while (fs.Read(data, 0, 2) > 0) * { * int szStruct = VideoHead.GetShort(data, 0); * if (szStruct < 0) break; * fs.Read(data, 2, szStruct - 2); * int szData = VideoHead.GetInt(data, 6); * if (szData < 0) break; * int szCaption = VideoHead.GetShort(data, 12); * if (szCaption < 0) break; * fs.Seek(szData, System.IO.SeekOrigin.Current); * if (szCaption > 0 && fs.Length > fs.Position) * { * int c = fs.Read(data, 0, szCaption); * if (c == szCaption) * { * string s = GetCaption(data, 0, szCaption); * if (!captions.Contains(s)) * captions.Add(s); * } * } * } */ } catch { if (captions.Count == 0) { bad = true; } } } else { bad = true; } fs.Close(); }