public static void Add(ffmpeg ffmpeg, Action action) { Thread thread = null; thread = new Thread( delegate() { action.Invoke(); // Dispose Event lock (threads) { threads.Remove(ffmpeg); } }); // Start Event lock (threads) { threads.Add(ffmpeg); thread.Start(); } }
private void DownLoad(string typeData) { if (UseType.Count == 0) { return; } OnLog(typeData + ":" + DownLoadIndex); string connectionString = "Database=xiqu;Data Source=sh-cdb-oq3ldfii.sql.tencentcdb.com;Port=61697;UserId=xiqu;Password=@lls19830803;Charset=utf8;TreatTinyAsBoolean=false;Allow User Variables=True"; DbBase db = new DbBase(connectionString, DBType.MySQL); IDbConnection Mysql = db.Conn; Mysql.Open(); IDbCommand command = Mysql.CreateCommand(); command.CommandText = "SELECT * FROM `hlwidc_video` WHERE `isall`=2 AND `server`=2 AND `type`='" + typeData + "' ORDER BY RAND() LIMIT 1"; IDataReader reader = command.ExecuteReader(); string filename = ""; string VideoName = ""; while (reader.Read()) { VideoName = reader["title"].ToString(); try { object url = reader["video_path"]; if (url != null) { string Uri = url.ToString().Substring(1); Uri = Uri.Substring(0, Uri.LastIndexOf(".")); string VideoUrl = "https://h5vv6.video.qq.com/getinfo?callback=j&platform=11001&charge=0&otype=json&ehost=https%3A%2F%2Fview.inews.qq.com&sphls=0&sb=1&nocache=0&_rnd=1613111258363&guid=1613111258363&appVer=V2.0Build9496&vids=" + Uri + "&defaultfmt=auto&&_qv_rmt=VEZfwXxMA12593l4u=&_qv_rmt2=48nK/4IM1535169lQ=&sdtfrom=v3110&_=1613111256247&jsonpCallback=j"; WebClient web = new WebClient(); web.Encoding = Encoding.UTF8; string VideoInfp = web.DownloadString(VideoUrl); VideoInfp = VideoInfp.Substring(2); VideoInfp = VideoInfp.Substring(0, VideoInfp.Length - 1); Console.WriteLine(VideoInfp); VideoInfo v = JsonHelper.DeserializeObject <VideoInfo>(VideoInfp); if (v.msg == null) { OnLog(VideoName + "获取地址成功"); vi[] vData = v.vl.vi; foreach (vi vv in vData) { string fvkey = vv.fvkey; int br = vv.br; string ti = vv.ti; string DownUi = vv.ul.ui[0].url; string DownUrl = DownUi + vv.fn + "?vkey=" + fvkey + "&br=" + br + "&platform=2&fmt=auto&level=0&sdtfrom=v3110&guid=1613111258363"; Console.WriteLine(DownUrl); DateTime start = DateTime.Now; Uri uri = new Uri(DownUrl); filename = DateTime.Now.ToString("yyyyMMddHHmmss") + Static.RandomNum(10000, 99999) + ".mp4"; if (!Directory.Exists(Robot.path + @"\tmp\")) { Directory.CreateDirectory(Robot.path + @"\tmp\"); } filename = Robot.path + @"\tmp\" + filename; //指定url 下载文件 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(DownUrl); Stream stream = request.GetResponse().GetResponseStream(); //创建写入流 FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write); byte[] bytes = new byte[1024 * 1024]; int readCount = 0; while (true) { readCount = stream.Read(bytes, 0, bytes.Length); if (readCount <= 0) { break; } fs.Write(bytes, 0, readCount); fs.Flush(); } fs.Close(); stream.Close(); OnLog("下载文件成功,用时:" + (DateTime.Now - start).TotalSeconds + "秒" + filename); break; } } else { OnLog(reader["title"].ToString() + "获取地址失败"); } } } catch { filename = ""; } } reader.Close(); Mysql.Close(); if (filename == "") { DownLoad(typeData); } else { ffmpeg mpeg = new ffmpeg(); mpeg.title = VideoName; mpeg.len = 30; mpeg.RunText = false; string file = mpeg.Start(filename); File.Delete(filename); OnLog("正在推送[CQ:video,file=file:///" + file + "]"); foreach (uint gid in GroupTags[typeData]) { long SendResult = Cluster.Send(gid, "[CQ:video,file=file:///" + file + "]"); OnLog("[" + gid + "]" + (SendResult != 0?"成功":"失败")); } } }
static void Main(string[] args) { Config.Setup(); if (Config.TempDirectory.Exists == false) { Config.TempDirectory.Create(); } new Thread(ConsoleSystem.ConsoleUpdateThread).Start(); // Check New File in Main Thread while (true) { if (Config.TargetDirectory.Exists) { List <FileInfo> FileData = FileIO.DirSearch(Config.TargetDirectory); foreach (var item in FileData) { // Wait idle thread bacause of MaxThreadCount. while (Config.MaxThread <= ThreadList.Count) { Thread.Sleep(1000); } if (String.IsNullOrEmpty(item.Extension)) { continue; // if file don't have extension, skip } if (Config.ReadFileExtension.Contains(item.Extension.Substring(1))) { if (FileIO.CheckSubPath(Config.TempDirectory, item.Directory)) { continue; } Match resolution = Regex.Match(item.Name, "([0-9]+)[iIpP]"); if (resolution.Success == false) { continue; } if (Int32.Parse(resolution.Groups[1].Value) < Config.EncoderMinResolution) { continue; } FileInfo OutputFile = FileIO.Replace_Extension(item, Config.OutputFileExtension); FileInfo TempFile = FileIO.Replace_Directory(OutputFile, Config.TempDirectory); FileInfo LockFile = FileIO.Replace_Extension(item, "lock"); if (OutputFile.Exists) { continue; // If the target file already exists } if (TempFile.Exists) { continue; // If a file with the same name exists in the temporary folder } if (LockFile.Exists) { continue; // If another computer perform converting file? (Only 1 thread) } // Check Setting List ffmpeg ffmpeg = new ffmpeg(item); VideoSetting videoSetting = null; AudioSetting audioSetting = null; foreach (VideoSetting checkset in Config.VideoSettings) { if (checkset.OriginalCodec == VideoType.ALL || checkset.OriginalCodec == ffmpeg.VideoCodec) { videoSetting = checkset; } } foreach (AudioSetting checkset in Config.AudioSettings) { if (checkset.OriginalCodec == AudioType.ALL || checkset.OriginalCodec == ffmpeg.AudioCodec) { audioSetting = checkset; } } if (videoSetting != null || audioSetting != null) { ThreadList.Add(ffmpeg, delegate() { try { MessageItem Message = new MessageItem("Found File", item.Name); ConsoleSystem.AddMessage(Message); // Create Lock File FileIO.CreateFile(LockFile); Message.Update("Create Lock", LockFile.Name); Message.Update("Convert File", item.Name + " -> " + TempFile.Name); bool result = ffmpeg.Convert(videoSetting, audioSetting, TempFile); if (result == true) { Message.Update("Move File", TempFile.Name + " -> " + OutputFile.Name); TempFile.MoveTo(OutputFile.FullName); Message.Update("Delete File", item.Name); item.Delete(); Message.Update("Complete", item.Name); } else { Message.Update("Error", item.Name); } } catch (Exception e) { if (TempFile.Exists) { TempFile.Delete(); } } finally { if (LockFile.Exists) { LockFile.Delete(); } } }); } } } Thread.Sleep(Config.ResearchTime * 1000); } } }