private void CloseReader(bool _null) { lock (locker) { if (reader != null) { reader.Close(); if (_null) { reader = null; } } } }
private void CloseReader() { lock (locker) { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); buffer = IntPtr.Zero; } if (reader != null) { reader.Close(); reader = null; } } }
public static string CheckScriptErrors(Massive m) { string er = null; AviSynthReader reader = new AviSynthReader(); try { reader.ParseScript(m.script); } catch (Exception ex) { er = ex.Message; } reader.Close(); reader = null; return er; }
public void UnloadAviSynth() { lock (locker) { if (reader != null) { try { reader.Close(); } //catch (Exception) { } //Всё-равно вылезет в ~AviSynthClip() finally { reader = null; if (!IsError && !IsAborted && AvsStateChanged != null) { OwnerDispatcher.Invoke(Priority, (ThreadStart) delegate() { AvsStateChanged(this, false); }); } } } } }
public Task(string thm, TaskStatus status, Massive mass) { _info = ""; _thm = thm; _status = status; _name = Path.GetFileName(mass.outfilepath); if (mass.outvcodec != "Copy" && mass.outvcodec != "Disabled") { //Переопределение некоторых параметров видео на основе текущего скрипта (т.к. он мог быть изменен вручную) if (Settings.ReadScript) //если в настройках это разрешено { bool recalc_sar = false; AviSynthReader reader = null; try { reader = new AviSynthReader(); reader.ParseScript(mass.script); if (mass.outresw != reader.Width) { if (mass.outvcodec == "x264" && Calculate.GetScriptBitDepth(mass.script) != 8) { if (mass.outresw != reader.Width / 2) { mass.outresw = reader.Width / 2; recalc_sar = true; } } else { mass.outresw = reader.Width; recalc_sar = true; } } if (mass.outresh != reader.Height) { mass.outresh = reader.Height; recalc_sar = true; } mass.outframes = reader.FrameCount; mass.outframerate = Calculate.ConvertDoubleToPointString(reader.Framerate); mass.outduration = TimeSpan.FromSeconds((double)mass.outframes / reader.Framerate); mass.outfilesize = Calculate.GetEncodingSize(mass); } catch { } finally { reader.Close(); reader = null; } //Переопределяем SAR в случае анаморфного кодирования (если разрешение поменялось) if (recalc_sar && mass.sar != null && mass.sar != "1:1") { mass = Calculate.CalculateSAR(mass); } } _info += mass.outresw + "x" + mass.outresh; _info += " " + mass.outframerate; if (mass.encodingmode == Settings.EncodingModes.Quality || mass.encodingmode == Settings.EncodingModes.Quantizer || mass.encodingmode == Settings.EncodingModes.TwoPassQuality || mass.encodingmode == Settings.EncodingModes.ThreePassQuality) { _info += " Q" + Calculate.ConvertDoubleToPointString((double)mass.outvbitrate, 1); } else if (mass.encodingmode == Settings.EncodingModes.TwoPassSize || mass.encodingmode == Settings.EncodingModes.ThreePassSize || mass.encodingmode == Settings.EncodingModes.OnePassSize) { _info += " " + mass.outvbitrate + "mb"; } else { _info += " " + mass.outvbitrate + "kbps"; } } else if (mass.outvcodec == "Copy") { _info += mass.inresw + "x" + mass.inresh; _info += " " + mass.inframerate; _info += " " + mass.invbitrate + "kbps"; } if (mass.outaudiostreams.Count > 0) { AudioStream outstream = (AudioStream)mass.outaudiostreams[mass.outaudiostream]; if (outstream.codec != "Copy") { _info += " " + outstream.samplerate + "Hz"; _info += " " + Calculate.ExplainChannels(outstream.channels); _info += " " + ((outstream.bitrate > 0) ? outstream.bitrate + "kbps" : "VBR"); if (mass.volume != "Disabled") { _info += " " + mass.volume; } } else { if (mass.inaudiostreams.Count > 0) { AudioStream instream = (AudioStream)mass.inaudiostreams[mass.inaudiostream]; _info += " " + instream.samplerate + "Hz"; _info += " " + Calculate.ExplainChannels(instream.channels); _info += " " + instream.bitrate + "kbps"; } } } _info += " " + ((mass.outfilesize != Languages.Translate("Unknown")) ? mass.outfilesize.Replace(" ", "") : "?mb"); _mass = mass.Clone(); _id = mass.key; }
private void make_thm(int width, int height, bool fix_ar, string format) { Bitmap bmp = null; Graphics g = null; AviSynthReader reader = null; string new_script = m.script; string thmpath = Calculate.RemoveExtention(m.outfilepath) + format; SetLog("CREATING THM"); SetLog("------------------------------"); SetLog("Saving picture to: " + thmpath); SetLog(format.ToUpper() + " " + width + "x" + height + " \r\n"); try { if (fix_ar) { int crop_w = 0, crop_h = 0; double old_asp = m.outaspect; double new_asp = (double)width / (double)height; if (old_asp < new_asp) { crop_h = Math.Max(Convert.ToInt32((m.outresh - ((m.outresh * old_asp) / new_asp)) / 2), 0); } else if (old_asp > new_asp) { crop_w = Math.Max(Convert.ToInt32((m.outresw - (m.outresw / old_asp) * new_asp) / 2), 0); } new_script += ("Lanczos4Resize(" + width + ", " + height + ", " + crop_w + ", " + crop_h + ", -" + crop_w + ", -" + crop_h + ")\r\n"); } reader = new AviSynthReader(AviSynthColorspace.RGB24, AudioSampleType.Undefined); reader.ParseScript(new_script); //проверка на выходы за пределы общего количества кадров int frame = (m.thmframe > reader.FrameCount) ? reader.FrameCount / 2 : m.thmframe; if (width == reader.Width && height == reader.Height) { bmp = new System.Drawing.Bitmap(reader.ReadFrameBitmap(frame)); } else { bmp = new Bitmap(width, height); g = Graphics.FromImage(bmp); //метод интерполяции при ресайзе g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(reader.ReadFrameBitmap(frame), 0, 0, width, height); } if (format == "jpg") { //процент cжатия jpg System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1); encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L); //jpg bmp.Save(thmpath, info[1], encoderParameters); } else if (format == "png") { //png bmp.Save(thmpath, System.Drawing.Imaging.ImageFormat.Png); } else { //bmp bmp.Save(thmpath, System.Drawing.Imaging.ImageFormat.Bmp); } } catch (Exception ex) { SetLog("\r\nError creating THM: " + ex.Message.ToString() + Environment.NewLine); } finally { //завершение if (g != null) { g.Dispose(); g = null; } if (bmp != null) { bmp.Dispose(); bmp = null; } if (reader != null) { reader.Close(); reader = null; } SetLog(""); } }
public Task(string thm, TaskStatus status, Massive mass) { _info = ""; _thm = thm; _status = status; _name = Path.GetFileName(mass.outfilepath); if (mass.outvcodec != "Copy" && mass.outvcodec != "Disabled") { //Переопределение некоторых параметров видео на основе текущего скрипта (т.к. он мог быть изменен вручную) if (Settings.ReadScript) //если в настройках это разрешено { bool recalc_sar = false; AviSynthReader reader = null; try { reader = new AviSynthReader(); reader.ParseScript(mass.script); if (mass.outresw != reader.Width) { if (mass.outvcodec == "x264" && Calculate.GetScriptBitDepth(mass.script) != 8) { if (mass.outresw != reader.Width / 2) { mass.outresw = reader.Width / 2; recalc_sar = true; } } else { mass.outresw = reader.Width; recalc_sar = true; } } if (mass.outresh != reader.Height) { mass.outresh = reader.Height; recalc_sar = true; } mass.outframes = reader.FrameCount; mass.outframerate = Calculate.ConvertDoubleToPointString(reader.Framerate); mass.outduration = TimeSpan.FromSeconds((double)mass.outframes / reader.Framerate); mass.outfilesize = Calculate.GetEncodingSize(mass); } catch { } finally { reader.Close(); reader = null; } //Переопределяем SAR в случае анаморфного кодирования (если разрешение поменялось) if (recalc_sar && mass.sar != null && mass.sar != "1:1") { mass = Calculate.CalculateSAR(mass); } } _info += mass.outresw + "x" + mass.outresh; _info += " " + mass.outframerate; if (mass.encodingmode == Settings.EncodingModes.Quality || mass.encodingmode == Settings.EncodingModes.Quantizer || mass.encodingmode == Settings.EncodingModes.TwoPassQuality || mass.encodingmode == Settings.EncodingModes.ThreePassQuality) _info += " Q" + Calculate.ConvertDoubleToPointString((double)mass.outvbitrate, 1); else if (mass.encodingmode == Settings.EncodingModes.TwoPassSize || mass.encodingmode == Settings.EncodingModes.ThreePassSize || mass.encodingmode == Settings.EncodingModes.OnePassSize) _info += " " + mass.outvbitrate + "mb"; else _info += " " + mass.outvbitrate + "kbps"; } else if (mass.outvcodec == "Copy") { _info += mass.inresw + "x" + mass.inresh; _info += " " + mass.inframerate; _info += " " + mass.invbitrate + "kbps"; } if (mass.outaudiostreams.Count > 0) { AudioStream outstream = (AudioStream)mass.outaudiostreams[mass.outaudiostream]; if (outstream.codec != "Copy") { _info += " " + outstream.samplerate + "Hz"; _info += " " + Calculate.ExplainChannels(outstream.channels); _info += " " + ((outstream.bitrate > 0) ? outstream.bitrate + "kbps" : "VBR"); if (mass.volume != "Disabled") _info += " " + mass.volume; } else { if (mass.inaudiostreams.Count > 0) { AudioStream instream = (AudioStream)mass.inaudiostreams[mass.inaudiostream]; _info += " " + instream.samplerate + "Hz"; _info += " " + Calculate.ExplainChannels(instream.channels); _info += " " + instream.bitrate + "kbps"; } } } _info += " " + ((mass.outfilesize != Languages.Translate("Unknown")) ? mass.outfilesize.Replace(" ", "") : "?mb"); _mass = mass.Clone(); _id = mass.key; }
private void UpdateScriptAndDuration() { //Обновляем скрипт m = AviSynthScripting.CreateAutoAviSynthScript(m); LoadVideo(MediaLoad.update); //Пересчет кол-ва кадров в видео, его продолжительности и размера получаемого файла int outframes = 0; TimeSpan outduration = TimeSpan.Zero; AviSynthReader reader = null; bool is_errors = false; try { reader = new AviSynthReader(); reader.ParseScript(m.script); outframes = reader.FrameCount; outduration = TimeSpan.FromSeconds((double)outframes / reader.Framerate); // / fps) } catch { is_errors = true; } finally { reader.Close(); reader = null; } if (!is_errors) { m.outframes = outframes; m.outduration = outduration; //TimeSpan.FromSeconds((double)m.outframes / fps); m.thmframe = m.outframes / 2; } else m = Calculate.UpdateOutFrames(m); m.outfilesize = Calculate.GetEncodingSize(m); UpdateTaskMassive(m); }
private void SavePicture(string path, int width, int height, bool fix_ar, int frame) { System.Drawing.Bitmap bmp = null; System.Drawing.Graphics g = null; AviSynthReader reader = null; string new_script = m.script; try { if (fix_ar && width != 0 && height != 0) { int crop_w = 0, crop_h = 0; double old_asp = m.outaspect; double new_asp = (double)width / (double)height; if (old_asp < new_asp) { crop_h = Math.Max(Convert.ToInt32((m.outresh - ((m.outresh * old_asp) / new_asp)) / 2), 0); } else if (old_asp > new_asp) { crop_w = Math.Max(Convert.ToInt32((m.outresw - (m.outresw / old_asp) * new_asp) / 2), 0); } new_script += ("Lanczos4Resize(" + width + ", " + height + ", " + crop_w + ", " + crop_h + ", -" + crop_w + ", -" + crop_h + ")\r\n"); } reader = new AviSynthReader(AviSynthColorspace.RGB24, AudioSampleType.Undefined); reader.ParseScript(new_script); if (width == 0 || height == 0 || (width == reader.Width && height == reader.Height)) { bmp = new System.Drawing.Bitmap(reader.ReadFrameBitmap(frame)); } else { bmp = new System.Drawing.Bitmap(width, height); g = System.Drawing.Graphics.FromImage(bmp); //метод интерполяции при ресайзе g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(reader.ReadFrameBitmap(frame), 0, 0, width, height); } string ext = Path.GetExtension(path).ToLower(); if (ext == ".jpg") { //процент cжатия jpg System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1); encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95L); //jpg bmp.Save(path, info[1], encoderParameters); } else if (ext == ".png") { //png bmp.Save(path, System.Drawing.Imaging.ImageFormat.Png); } else { //bmp bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); } } catch (Exception ex) { ErrorException("SavePicture: " + ex.Message, ex.StackTrace); } finally { //завершение if (g != null) { g.Dispose(); g = null; } if (bmp != null) { bmp.Dispose(); bmp = null; } if (reader != null) { reader.Close(); reader = null; } } }