Пример #1
0
        public ScreenRecorder(ScreenRecordOutput outputType, ScreenRecordingOptions options, Screenshot screenshot, Rectangle captureRectangle)
        {
            if (string.IsNullOrEmpty(options.OutputPath))
            {
                throw new Exception("Screen recorder cache path is empty.");
            }

            FPS              = options.FPS;
            DurationSeconds  = options.Duration;
            CaptureRectangle = captureRectangle;
            CachePath        = options.OutputPath;
            OutputType       = outputType;

            Options = options;

            switch (OutputType)
            {
            default:
            case ScreenRecordOutput.FFmpeg:
                FileHelpers.CreateDirectoryFromFilePath(Options.OutputPath);
                ffmpeg                        = new FFmpegCLIManager(Options.FFmpeg.FFmpegPath);
                ffmpeg.ShowError              = true;
                ffmpeg.EncodeStarted         += OnRecordingStarted;
                ffmpeg.EncodeProgressChanged += OnEncodingProgressChanged;
                break;

            case ScreenRecordOutput.GIF:
                imgCache = new HardDiskCache(Options);
                break;
            }

            this.screenshot = screenshot;
        }
Пример #2
0
        public void SaveAsGIF(string path, GIFQuality quality)
        {
            if (imgCache != null && imgCache is HardDiskCache && !IsRecording)
            {
                FileHelpers.CreateDirectoryFromFilePath(path);

                HardDiskCache hdCache = imgCache as HardDiskCache;

                using (AnimatedGifCreator gifEncoder = new AnimatedGifCreator(path, delay))
                {
                    int i     = 0;
                    int count = hdCache.Count;

                    foreach (Image img in hdCache.GetImageEnumerator())
                    {
                        i++;
                        OnEncodingProgressChanged((int)((float)i / count * 100));

                        using (img)
                        {
                            gifEncoder.AddFrame(img, quality);
                        }
                    }
                }
            }
        }
Пример #3
0
 public HardDiskCache(ScreenRecordingOptions options)
 {
     Options = options;
     FileHelpers.CreateDirectoryFromFilePath(Options.OutputPath);
     fsCache   = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read);
     indexList = new List <LocationInfo>();
 }
Пример #4
0
        private static void CreateFirefoxHostManifest(string filePath)
        {
            FileHelpers.CreateDirectoryFromFilePath(filePath);

            FirefoxManifest manifest = new FirefoxManifest()
            {
                name               = "ShareX",
                description        = "ShareX",
                path               = Program.NativeMessagingHostFilePath,
                type               = "stdio",
                allowed_extensions = new string[] { "*****@*****.**" }
            };

            string json = JsonConvert.SerializeObject(manifest, Formatting.Indented);

            File.WriteAllText(filePath, json, Encoding.UTF8);
        }
Пример #5
0
        private static void CreateChromeHostManifest(string filePath)
        {
            FileHelpers.CreateDirectoryFromFilePath(filePath);

            ChromeManifest manifest = new ChromeManifest()
            {
                name            = "com.getsharex.sharex",
                description     = "ShareX",
                path            = Program.NativeMessagingHostFilePath,
                type            = "stdio",
                allowed_origins = new string[] { "chrome-extension://nlkoigbdolhchiicbonbihbphgamnaoc/" }
            };

            string json = JsonConvert.SerializeObject(manifest, Formatting.Indented);

            File.WriteAllText(filePath, json, Encoding.UTF8);
        }
Пример #6
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            UploadResult result = new UploadResult();

            string filePath = account.GetLocalhostPath(fileName);

            FileHelpers.CreateDirectoryFromFilePath(filePath);

            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                if (TransferData(stream, fs))
                {
                    result.URL = account.GetUriPath(Path.GetFileName(fileName));
                }
            }

            return(result);
        }
Пример #7
0
        private void DoTextJobs()
        {
            if (Info.TaskSettings.AdvancedSettings.TextTaskSaveAsFile)
            {
                string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings);
                string filePath          = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings);

                if (!string.IsNullOrEmpty(filePath))
                {
                    Info.FilePath = filePath;
                    FileHelpers.CreateDirectoryFromFilePath(Info.FilePath);
                    File.WriteAllText(Info.FilePath, Text, Encoding.UTF8);
                    DebugHelper.WriteLine("Text saved to file: " + Info.FilePath);
                }
            }

            byte[] byteArray = Encoding.UTF8.GetBytes(Text);
            Data = new MemoryStream(byteArray);
        }
Пример #8
0
        private bool DownloadFromURL(bool upload)
        {
            string url = Info.Result.URL.Trim();

            Info.Result.URL = "";

            string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings);

            Info.FilePath = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings);

            if (!string.IsNullOrEmpty(Info.FilePath))
            {
                Info.Status = Resources.UploadTask_DownloadAndUpload_Downloading;
                OnStatusChanged();

                try
                {
                    FileHelpers.CreateDirectoryFromFilePath(Info.FilePath);

                    using (WebClient wc = new WebClient())
                    {
                        wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent);
                        wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy();
                        wc.DownloadFile(url, Info.FilePath);
                    }

                    if (upload)
                    {
                        LoadFileStream();
                    }

                    return(true);
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e);
                    MessageBox.Show(string.Format(Resources.UploadManager_DownloadAndUploadFile_Download_failed, e), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return(false);
        }
Пример #9
0
        public bool FFmpegEncodeAsGIF(string input, string output)
        {
            FileHelpers.CreateDirectoryFromFilePath(output);

            try
            {
                ffmpeg.TrackEncodeProgress = true;

                // https://ffmpeg.org/ffmpeg-filters.html#palettegen-1
                // https://ffmpeg.org/ffmpeg-filters.html#paletteuse
                return(ffmpeg.Run($"-hide_banner -i \"{input}\" -lavfi \"palettegen=stats_mode={Options.FFmpeg.GIFStatsMode}[palette]," +
                                  $"[0:v][palette]paletteuse=dither={Options.FFmpeg.GIFDither}" +
                                  $"{(Options.FFmpeg.GIFDither == FFmpegPaletteUseDither.bayer ? ":bayer_scale=" + Options.FFmpeg.GIFBayerScale : "")}\"" +
                                  $" -y \"{output}\""));
            }
            finally
            {
                ffmpeg.TrackEncodeProgress = false;
            }
        }
Пример #10
0
        public bool FFmpegEncodeVideo(string input, string output)
        {
            FileHelpers.CreateDirectoryFromFilePath(output);

            Options.IsRecording = false;
            Options.IsLossless  = false;
            Options.InputPath   = input;
            Options.OutputPath  = output;

            try
            {
                ffmpeg.TrackEncodeProgress = true;

                return(ffmpeg.Run(Options.GetFFmpegCommands()));
            }
            finally
            {
                ffmpeg.TrackEncodeProgress = false;
            }
        }
Пример #11
0
        protected override bool Append(string filePath, IEnumerable <HistoryItem> historyItems)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                lock (thisLock)
                {
                    FileHelpers.CreateDirectoryFromFilePath(filePath);

                    using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                        using (StreamWriter streamWriter = new StreamWriter(fileStream))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            serializer.DefaultValueHandling = DefaultValueHandling.Ignore;

                            bool firstObject = fileStream.Length == 0;

                            foreach (HistoryItem historyItem in historyItems)
                            {
                                string json = "";

                                if (!firstObject)
                                {
                                    json += ",\r\n";
                                }

                                json += JObject.FromObject(historyItem, serializer).ToString();

                                streamWriter.Write(json);

                                firstObject = false;
                            }
                        }

                    Backup(FilePath);
                }

                return(true);
            }

            return(false);
        }
Пример #12
0
        protected override bool Append(string filePath, IEnumerable <HistoryItem> historyItems)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                lock (thisLock)
                {
                    FileHelpers.CreateDirectoryFromFilePath(filePath);

                    using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                        using (XmlTextWriter writer = new XmlTextWriter(fileStream, Encoding.UTF8))
                        {
                            writer.Formatting  = Formatting.Indented;
                            writer.Indentation = 4;

                            foreach (HistoryItem historyItem in historyItems)
                            {
                                writer.WriteStartElement("HistoryItem");
                                writer.WriteElementIfNotEmpty("Filename", historyItem.FileName);
                                writer.WriteElementIfNotEmpty("Filepath", historyItem.FilePath);
                                writer.WriteElementIfNotEmpty("DateTimeUtc", historyItem.DateTime.ToString("o"));
                                writer.WriteElementIfNotEmpty("Type", historyItem.Type);
                                writer.WriteElementIfNotEmpty("Host", historyItem.Host);
                                writer.WriteElementIfNotEmpty("URL", historyItem.URL);
                                writer.WriteElementIfNotEmpty("ThumbnailURL", historyItem.ThumbnailURL);
                                writer.WriteElementIfNotEmpty("DeletionURL", historyItem.DeletionURL);
                                writer.WriteElementIfNotEmpty("ShortenedURL", historyItem.ShortenedURL);
                                writer.WriteEndElement();
                            }

                            writer.WriteWhitespace(Environment.NewLine);
                        }

                    Backup(FilePath);
                }

                return(true);
            }

            return(false);
        }
Пример #13
0
        public void AddWatchFolder(WatchFolderSettings watchFolderSetting, TaskSettings taskSettings)
        {
            if (!IsExist(watchFolderSetting))
            {
                if (!taskSettings.WatchFolderList.Contains(watchFolderSetting))
                {
                    taskSettings.WatchFolderList.Add(watchFolderSetting);
                }

                WatchFolder watchFolder = new WatchFolder();
                watchFolder.Settings     = watchFolderSetting;
                watchFolder.TaskSettings = taskSettings;

                watchFolder.FileWatcherTrigger += origPath =>
                {
                    TaskSettings taskSettingsCopy = TaskSettings.GetSafeTaskSettings(taskSettings);
                    string       destPath         = origPath;

                    if (watchFolderSetting.MoveFilesToScreenshotsFolder)
                    {
                        string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettingsCopy);
                        string fileName          = Path.GetFileName(origPath);
                        destPath = TaskHelpers.HandleExistsFile(screenshotsFolder, fileName, taskSettingsCopy);
                        FileHelpers.CreateDirectoryFromFilePath(destPath);
                        File.Move(origPath, destPath);
                    }

                    UploadManager.UploadFile(destPath, taskSettingsCopy);
                };

                WatchFolders.Add(watchFolder);

                if (taskSettings.WatchFolderEnabled)
                {
                    watchFolder.Enable();
                }
            }
        }