public static async Task<EncodingHelper> GetDBCSEncoding(string name)
        {
            name = name.ToLower();
            EncodingHelper encoding = new EncodingHelper();
            encoding._webName = name;
            if (_cache.ContainsKey(name))
            {
                var tuple = _cache[name];
                encoding._dbcsToUnicode = tuple.Item1;
                encoding._unicodeToDbcs = tuple.Item2;
                return encoding;
            }

            var dbcsToUnicode = new char[0x10000];
            var unicodeToDbcs = new ushort[0x10000];

            using (Stream stream = await GetInstall())
            using (BinaryReader reader = new BinaryReader(stream))
            {
                for (int i = 0; i < 0xffff; i++)
                {
                    ushort u = reader.ReadUInt16();
                    unicodeToDbcs[i] = u;
                }
                for (int i = 0; i < 0xffff; i++)
                {
                    ushort u = reader.ReadUInt16();
                    dbcsToUnicode[i] = (char)u;
                }
            }

            _cache[name] = new Tuple<char[], ushort[]>(dbcsToUnicode, unicodeToDbcs);
            encoding._dbcsToUnicode = dbcsToUnicode;
            encoding._unicodeToDbcs = unicodeToDbcs;
            return encoding;
        }
Esempio n. 2
0
        private async Task <string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int?imageStreamIndex, Video3DFormat?threedFormat, TimeSpan?offset, bool useIFrame, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(inputPath))
            {
                throw new ArgumentNullException(nameof(inputPath));
            }

            var tempExtractPath = Path.Combine(ConfigurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".jpg");

            Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));

            // apply some filters to thumbnail extracted below (below) crop any black lines that we made and get the correct ar then scale to width 600.
            // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar
            var vf = "scale=600:trunc(600/dar/2)*2";

            if (threedFormat.HasValue)
            {
                switch (threedFormat.Value)
                {
                case Video3DFormat.HalfSideBySide:
                    vf = "crop=iw/2:ih:0:0,scale=(iw*2):ih,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
                    // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars we may have made the scale width to 600. Work out the correct height based on the display aspect it will maintain the aspect where -1 in this case (3d) may not.
                    break;

                case Video3DFormat.FullSideBySide:
                    vf = "crop=iw/2:ih:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
                    //fsbs crop width in half,set the display aspect,crop out any black bars we may have made the scale width to 600.
                    break;

                case Video3DFormat.HalfTopAndBottom:
                    vf = "crop=iw:ih/2:0:0,scale=(iw*2):ih),setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
                    //htab crop heigh in half,scale to correct size, set the display aspect,crop out any black bars we may have made the scale width to 600
                    break;

                case Video3DFormat.FullTopAndBottom:
                    vf = "crop=iw:ih/2:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
                    // ftab crop heigt in half, set the display aspect,crop out any black bars we may have made the scale width to 600
                    break;

                default:
                    break;
                }
            }

            var mapArg = imageStreamIndex.HasValue ? (" -map 0:v:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty;

            var enableThumbnail = !new List <string> {
                "wtv"
            }.Contains(container ?? string.Empty, StringComparer.OrdinalIgnoreCase);
            // Use ffmpeg to sample 100 (we can drop this if required using thumbnail=50 for 50 frames) frames and pick the best thumbnail. Have a fall back just in case.
            var thumbnail = enableThumbnail ? ",thumbnail=24" : string.Empty;

            var args = useIFrame ? string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}{4}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg, thumbnail) :
                       string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg);

            var probeSizeArgument       = EncodingHelper.GetProbeSizeArgument(1);
            var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1);

            if (!string.IsNullOrWhiteSpace(probeSizeArgument))
            {
                args = probeSizeArgument + " " + args;
            }

            if (!string.IsNullOrWhiteSpace(analyzeDurationArgument))
            {
                args = analyzeDurationArgument + " " + args;
            }

            if (offset.HasValue)
            {
                args = string.Format("-ss {0} ", GetTimeParameter(offset.Value)) + args;
            }

            var encodinghelper = new EncodingHelper(this, FileSystem, SubtitleEncoder());

            if (videoStream != null)
            {
                /* fix
                 * var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions());
                 * if (!string.IsNullOrWhiteSpace(decoder))
                 * {
                 *  args = decoder + " " + args;
                 * }
                 */
            }

            if (!string.IsNullOrWhiteSpace(container))
            {
                var inputFormat = encodinghelper.GetInputFormat(container);
                if (!string.IsNullOrWhiteSpace(inputFormat))
                {
                    args = "-f " + inputFormat + " " + args;
                }
            }

            var process = _processFactory.Create(new ProcessOptions
            {
                CreateNoWindow      = true,
                UseShellExecute     = false,
                FileName            = FFmpegPath,
                Arguments           = args,
                IsHidden            = true,
                ErrorDialog         = false,
                EnableRaisingEvents = true
            });

            _logger.LogDebug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);

            using (var processWrapper = new ProcessWrapper(process, this, _logger))
            {
                bool ranToCompletion;

                StartProcess(processWrapper);

                var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs;
                if (timeoutMs <= 0)
                {
                    timeoutMs = DefaultImageExtractionTimeoutMs;
                }

                ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false);

                if (!ranToCompletion)
                {
                    StopProcess(processWrapper, 1000);
                }

                var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
                var file     = FileSystem.GetFileInfo(tempExtractPath);

                if (exitCode == -1 || !file.Exists || file.Length == 0)
                {
                    var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath);

                    _logger.LogError(msg);

                    throw new Exception(msg);
                }

                return(tempExtractPath);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>StreamState.</returns>
        protected async Task <StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
        {
            ParseDlnaHeaders(request);

            if (!string.IsNullOrWhiteSpace(request.Params))
            {
                ParseParams(request);
            }

            ParseStreamOptions(request);

            var url = Request.PathInfo;

            if (string.IsNullOrEmpty(request.AudioCodec))
            {
                request.AudioCodec = EncodingHelper.InferAudioCodec(url);
            }

            var enableDlnaHeaders = !string.IsNullOrWhiteSpace(request.Params) ||
                                    string.Equals(GetHeader("GetContentFeatures.DLNA.ORG"), "1", StringComparison.OrdinalIgnoreCase);

            var state = new StreamState(MediaSourceManager, TranscodingJobType)
            {
                Request           = request,
                RequestedUrl      = url,
                UserAgent         = Request.UserAgent,
                EnableDlnaHeaders = enableDlnaHeaders
            };

            var auth = AuthorizationContext.GetAuthorizationInfo(Request);

            if (!auth.UserId.Equals(Guid.Empty))
            {
                state.User = UserManager.GetUserById(auth.UserId);
            }

            //if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
            //    (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
            //    (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
            //{
            //    state.SegmentLength = 6;
            //}

            if (state.VideoRequest != null && !string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec))
            {
                state.SupportedVideoCodecs    = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
                state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
            }

            if (!string.IsNullOrWhiteSpace(request.AudioCodec))
            {
                state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
                state.Request.AudioCodec   = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i))
                                             ?? state.SupportedAudioCodecs.FirstOrDefault();
            }

            if (!string.IsNullOrWhiteSpace(request.SubtitleCodec))
            {
                state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray();
                state.Request.SubtitleCodec   = state.SupportedSubtitleCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToSubtitleCodec(i))
                                                ?? state.SupportedSubtitleCodecs.FirstOrDefault();
            }

            var item = LibraryManager.GetItemById(request.Id);

            state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);

            //var primaryImage = item.GetImageInfo(ImageType.Primary, 0) ??
            //             item.Parents.Select(i => i.GetImageInfo(ImageType.Primary, 0)).FirstOrDefault(i => i != null);
            //if (primaryImage != null)
            //{
            //    state.AlbumCoverPath = primaryImage.Path;
            //}

            MediaSourceInfo mediaSource = null;

            if (string.IsNullOrWhiteSpace(request.LiveStreamId))
            {
                var currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ?
                                 ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId)
                    : null;

                if (currentJob != null)
                {
                    mediaSource = currentJob.MediaSource;
                }

                if (mediaSource == null)
                {
                    var mediaSources = await MediaSourceManager.GetPlaybackMediaSources(LibraryManager.GetItemById(request.Id), null, false, false, cancellationToken).ConfigureAwait(false);

                    mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
                       ? mediaSources[0]
                       : mediaSources.Find(i => string.Equals(i.Id, request.MediaSourceId));

                    if (mediaSource == null && Guid.Parse(request.MediaSourceId) == request.Id)
                    {
                        mediaSource = mediaSources[0];
                    }
                }
            }
            else
            {
                var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false);

                mediaSource = liveStreamInfo.Item1;
                state.DirectStreamProvider = liveStreamInfo.Item2;
            }

            var videoRequest = request as VideoStreamRequest;

            EncodingHelper.AttachMediaSourceInfo(state, mediaSource, url);

            var container = Path.GetExtension(state.RequestedUrl);

            if (string.IsNullOrEmpty(container))
            {
                container = request.Container;
            }

            if (string.IsNullOrEmpty(container))
            {
                container = request.Static ?
                            StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(state.InputContainer, state.MediaPath, null, DlnaProfileType.Audio) :
                            GetOutputFileExtension(state);
            }

            state.OutputContainer = (container ?? string.Empty).TrimStart('.');

            state.OutputAudioBitrate = EncodingHelper.GetAudioBitrateParam(state.Request, state.AudioStream);

            state.OutputAudioCodec = state.Request.AudioCodec;

            state.OutputAudioChannels = EncodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec);

            if (videoRequest != null)
            {
                state.OutputVideoCodec   = state.VideoRequest.VideoCodec;
                state.OutputVideoBitrate = EncodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);

                if (videoRequest != null)
                {
                    EncodingHelper.TryStreamCopy(state);
                }

                if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
                {
                    var resolution = ResolutionNormalizer.Normalize(
                        state.VideoStream?.BitRate,
                        state.VideoStream?.Width,
                        state.VideoStream?.Height,
                        state.OutputVideoBitrate.Value,
                        state.VideoStream?.Codec,
                        state.OutputVideoCodec,
                        videoRequest.MaxWidth,
                        videoRequest.MaxHeight);

                    videoRequest.MaxWidth  = resolution.MaxWidth;
                    videoRequest.MaxHeight = resolution.MaxHeight;
                }
            }

            ApplyDeviceProfileSettings(state);

            var ext = string.IsNullOrWhiteSpace(state.OutputContainer)
                ? GetOutputFileExtension(state)
                : ('.' + state.OutputContainer);

            var encodingOptions = ServerConfigurationManager.GetEncodingOptions();

            state.OutputFilePath = GetOutputFilePath(state, encodingOptions, ext);

            return(state);
        }
Esempio n. 4
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     serializer.Serialize(writer, EncodingHelper.GetUnixTimestampMillis((DateTime)value));
 }
Esempio n. 5
0
        private async Task <string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int?imageStreamIndex, Video3DFormat?threedFormat, TimeSpan?offset, bool useIFrame, bool allowTonemap, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(inputPath))
            {
                throw new ArgumentNullException(nameof(inputPath));
            }

            var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".jpg");

            Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));

            // apply some filters to thumbnail extracted below (below) crop any black lines that we made and get the correct ar.
            // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar
            var vf = string.Empty;

            if (threedFormat.HasValue)
            {
                switch (threedFormat.Value)
                {
                case Video3DFormat.HalfSideBySide:
                    vf = "-vf crop=iw/2:ih:0:0,scale=(iw*2):ih,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1";
                    // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars we may have made. Work out the correct height based on the display aspect it will maintain the aspect where -1 in this case (3d) may not.
                    break;

                case Video3DFormat.FullSideBySide:
                    vf = "-vf crop=iw/2:ih:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1";
                    // fsbs crop width in half,set the display aspect,crop out any black bars we may have made
                    break;

                case Video3DFormat.HalfTopAndBottom:
                    vf = "-vf crop=iw:ih/2:0:0,scale=(iw*2):ih),setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1";
                    // htab crop heigh in half,scale to correct size, set the display aspect,crop out any black bars we may have made
                    break;

                case Video3DFormat.FullTopAndBottom:
                    vf = "-vf crop=iw:ih/2:0:0,setdar=dar=a,crop=min(iw\\,ih*dar):min(ih\\,iw/dar):(iw-min(iw\\,iw*sar))/2:(ih - min (ih\\,ih/sar))/2,setsar=sar=1";
                    // ftab crop heigt in half, set the display aspect,crop out any black bars we may have made
                    break;

                default:
                    break;
                }
            }

            var mapArg = imageStreamIndex.HasValue ? (" -map 0:v:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty;

            var enableHdrExtraction = allowTonemap && string.Equals(videoStream?.VideoRange, "HDR", StringComparison.OrdinalIgnoreCase);

            if (enableHdrExtraction)
            {
                string tonemapFilters = "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0:peak=100,zscale=t=bt709:m=bt709,format=yuv420p";
                if (string.IsNullOrEmpty(vf))
                {
                    vf = "-vf " + tonemapFilters;
                }
                else
                {
                    vf += "," + tonemapFilters;
                }
            }

            // Use ffmpeg to sample 100 (we can drop this if required using thumbnail=50 for 50 frames) frames and pick the best thumbnail. Have a fall back just in case.
            // mpegts need larger batch size otherwise the corrupted thumbnail will be created. Larger batch size will lower the processing speed.
            var enableThumbnail = useIFrame && !string.Equals("wtv", container, StringComparison.OrdinalIgnoreCase);

            if (enableThumbnail)
            {
                var useLargerBatchSize = string.Equals("mpegts", container, StringComparison.OrdinalIgnoreCase);
                var batchSize          = useLargerBatchSize ? "50" : "24";
                if (string.IsNullOrEmpty(vf))
                {
                    vf = "-vf thumbnail=" + batchSize;
                }
                else
                {
                    vf += ",thumbnail=" + batchSize;
                }
            }

            var args = string.Format(CultureInfo.InvariantCulture, "-i {0}{3} -threads {4} -v quiet -vframes 1 {2} -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg, threads);

            var probeSizeArgument       = string.Empty;
            var analyzeDurationArgument = string.Empty;

            if (!string.IsNullOrWhiteSpace(probeSizeArgument))
            {
                args = probeSizeArgument + " " + args;
            }

            if (!string.IsNullOrWhiteSpace(analyzeDurationArgument))
            {
                args = analyzeDurationArgument + " " + args;
            }

            if (offset.HasValue)
            {
                args = string.Format(CultureInfo.InvariantCulture, "-ss {0} ", GetTimeParameter(offset.Value)) + args;
            }

            if (videoStream != null)
            {
                /* fix
                 * var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions());
                 * if (!string.IsNullOrWhiteSpace(decoder))
                 * {
                 *  args = decoder + " " + args;
                 * }
                 */
            }

            if (!string.IsNullOrWhiteSpace(container))
            {
                var inputFormat = EncodingHelper.GetInputFormat(container);
                if (!string.IsNullOrWhiteSpace(inputFormat))
                {
                    args = "-f " + inputFormat + " " + args;
                }
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    CreateNoWindow  = true,
                    UseShellExecute = false,
                    FileName        = _ffmpegPath,
                    Arguments       = args,
                    WindowStyle     = ProcessWindowStyle.Hidden,
                    ErrorDialog     = false,
                },
                EnableRaisingEvents = true
            };

            _logger.LogDebug("{ProcessFileName} {ProcessArguments}", process.StartInfo.FileName, process.StartInfo.Arguments);

            using (var processWrapper = new ProcessWrapper(process, this))
            {
                bool ranToCompletion;

                await _thumbnailResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);

                try
                {
                    StartProcess(processWrapper);

                    var timeoutMs = _configurationManager.Configuration.ImageExtractionTimeoutMs;
                    if (timeoutMs <= 0)
                    {
                        timeoutMs = enableHdrExtraction ? DefaultHdrImageExtractionTimeout : DefaultSdrImageExtractionTimeout;
                    }

                    ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);

                    if (!ranToCompletion)
                    {
                        StopProcess(processWrapper, 1000);
                    }
                }
                finally
                {
                    _thumbnailResourcePool.Release();
                }

                var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
                var file     = _fileSystem.GetFileInfo(tempExtractPath);

                if (exitCode == -1 || !file.Exists || file.Length == 0)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture, "ffmpeg image extraction failed for {0}", inputPath);

                    _logger.LogError(msg);

                    throw new Exception(msg);
                }

                return(tempExtractPath);
            }
        }
Esempio n. 6
0
        public static List <LogInfo> TXTDelLineOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_TXTDelLineOp infoOp = cmd.Info.Cast <CodeInfo_TXTDelLineOp>();

            CodeInfo_TXTDelLine firstInfo = infoOp.Infos[0];
            string fileName = StringEscaper.Preprocess(s, firstInfo.FileName);

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            List <(CodeCommand, string)> prepDeleteLine = new List <(CodeCommand, string)>(infoOp.Cmds.Count);

            foreach (CodeCommand subCmd in infoOp.Cmds)
            {
                CodeInfo_TXTDelLine info = subCmd.Info.Cast <CodeInfo_TXTDelLine>();

                string deleteLine = StringEscaper.Preprocess(s, info.DeleteLine);
                prepDeleteLine.Add((subCmd, deleteLine));
            }

            // Detect encoding of text.
            Encoding encoding = EncodingHelper.SmartDetectEncoding(fileName, prepDeleteLine.Select(t => t.Item2));

            int    count    = 0;
            string tempPath = FileHelper.GetTempFile();

            using (StreamReader r = new StreamReader(fileName, encoding, false))
                using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
                {
                    string srcLine;
                    while ((srcLine = r.ReadLine()) != null)
                    {
                        bool writeLine = true;
                        foreach ((CodeCommand _, string deleteLine) in prepDeleteLine)
                        {
                            // Strange enough, WB082 treat [deleteLine] as case sensitive string.
                            if (srcLine.StartsWith(deleteLine, StringComparison.Ordinal))
                            {
                                writeLine = false;
                                count++;

                                break;
                            }
                        }

                        if (writeLine)
                        {
                            w.WriteLine(srcLine);
                        }
                    }
                }
            FileHelper.FileReplaceEx(tempPath, fileName);

            foreach ((CodeCommand subCmd, string deleteLine) in prepDeleteLine)
            {
                logs.Add(new LogInfo(LogState.Success, $"Line [{deleteLine}] deleted from [{fileName}]", subCmd));
            }
            logs.Add(new LogInfo(LogState.Success, $"Deleted [{count}] lines from [{fileName}]"));

            return(logs);
        }
Esempio n. 7
0
        public static List <LogInfo> TXTReplaceOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            CodeInfo_TXTReplaceOp infoOp = cmd.Info.Cast <CodeInfo_TXTReplaceOp>();

            CodeInfo_TXTReplace firstInfo = infoOp.Infos[0];
            string fileName = StringEscaper.Preprocess(s, firstInfo.FileName);

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            if (!File.Exists(fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"File [{fileName}] does not exist"));
            }

            List <(CodeCommand, string, string)> prepReplace = new List <(CodeCommand, string, string)>();

            // foreach (CodeInfo_TXTReplace info in infoOp.Infos)
            foreach (CodeCommand subCmd in infoOp.Cmds)
            {
                CodeInfo_TXTReplace info = subCmd.Info.Cast <CodeInfo_TXTReplace>();

                string oldStr = StringEscaper.Preprocess(s, info.OldStr);
                string newStr = StringEscaper.Preprocess(s, info.NewStr);

                prepReplace.Add((subCmd, oldStr, newStr));
            }

            // Detect encoding of text. If text does not exists, create blank file (ANSI)
            Encoding encoding;

            if (File.Exists(fileName))
            {
                encoding = EncodingHelper.SmartDetectEncoding(fileName, () =>
                {
                    return(infoOp.Infos.All(x => EncodingHelper.IsActiveCodePageCompatible(x.OldStr)) &&
                           infoOp.Infos.All(x => EncodingHelper.IsActiveCodePageCompatible(x.NewStr)));
                });
            }
            else
            {
                encoding = EncodingHelper.DefaultAnsi;
            }

            string tempPath = FileHelper.GetTempFile();
            string txtStr;

            using (StreamReader r = new StreamReader(fileName, encoding, false))
            {
                txtStr = r.ReadToEnd();
            }

            foreach ((CodeCommand subCmd, string oldStr, string newStr) in prepReplace)
            {
                txtStr = StringHelper.ReplaceEx(txtStr, oldStr, newStr, StringComparison.OrdinalIgnoreCase);
                logs.Add(new LogInfo(LogState.Success, $"Replaced [{oldStr}] with [{newStr}]", subCmd));
            }

            using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
            {
                w.Write(txtStr);
            }
            logs.Add(new LogInfo(LogState.Success, $"Replaced [{prepReplace.Count}] strings from [{fileName}]"));

            FileHelper.FileReplaceEx(tempPath, fileName);

            return(logs);
        }
Esempio n. 8
0
        protected override string GetCommandLineArguments(string outputPath, EncodingOptions encodingOptions, StreamState state, bool isEncoding)
        {
            var itsOffsetMs = 0;

            var itsOffset = itsOffsetMs == 0 ? string.Empty : string.Format("-itsoffset {0} ", TimeSpan.FromMilliseconds(itsOffsetMs).TotalSeconds.ToString(CultureInfo.InvariantCulture));

            var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions);

            var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, videoCodec);

            var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions);

            // If isEncoding is true we're actually starting ffmpeg
            var startNumberParam = isEncoding ? GetStartNumber(state).ToString(CultureInfo.InvariantCulture) : "0";

            var baseUrlParam = string.Empty;

            if (state.Request is GetLiveHlsStream)
            {
                baseUrlParam = string.Format(" -hls_base_url \"{0}/\"",
                                             "hls/" + Path.GetFileNameWithoutExtension(outputPath));
            }

            var useGenericSegmenter = true;

            if (useGenericSegmenter)
            {
                var outputTsArg = Path.Combine(Path.GetDirectoryName(outputPath), Path.GetFileNameWithoutExtension(outputPath)) + "%d" + GetSegmentFileExtension(state.Request);

                var timeDeltaParam = string.Empty;

                var segmentFormat = GetSegmentFileExtension(state.Request).TrimStart('.');
                if (string.Equals(segmentFormat, "ts", StringComparison.OrdinalIgnoreCase))
                {
                    segmentFormat = "mpegts";
                }

                baseUrlParam = string.Format("\"{0}/\"", "hls/" + Path.GetFileNameWithoutExtension(outputPath));

                return(string.Format("{0} {1} -map_metadata -1 -map_chapters -1 -threads {2} {3} {4} {5} -f segment -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero -segment_time {6} {10} -individual_header_trailer 0 -segment_format {11} -segment_list_entry_prefix {12} -segment_list_type m3u8 -segment_start_number {7} -segment_list \"{8}\" -y \"{9}\"",
                                     inputModifier,
                                     EncodingHelper.GetInputArgument(state, encodingOptions),
                                     threads,
                                     EncodingHelper.GetMapArgs(state),
                                     GetVideoArguments(state, encodingOptions),
                                     GetAudioArguments(state, encodingOptions),
                                     state.SegmentLength.ToString(CultureInfo.InvariantCulture),
                                     startNumberParam,
                                     outputPath,
                                     outputTsArg,
                                     timeDeltaParam,
                                     segmentFormat,
                                     baseUrlParam
                                     ).Trim());
            }

            // add when stream copying?
            // -avoid_negative_ts make_zero -fflags +genpts

            var args = string.Format("{0} {1} {2} -map_metadata -1 -map_chapters -1 -threads {3} {4} {5} -max_delay 5000000 -avoid_negative_ts disabled -start_at_zero {6} -hls_time {7} -individual_header_trailer 0 -start_number {8} -hls_list_size {9}{10} -y \"{11}\"",
                                     itsOffset,
                                     inputModifier,
                                     EncodingHelper.GetInputArgument(state, encodingOptions),
                                     threads,
                                     EncodingHelper.GetMapArgs(state),
                                     GetVideoArguments(state, encodingOptions),
                                     GetAudioArguments(state, encodingOptions),
                                     state.SegmentLength.ToString(CultureInfo.InvariantCulture),
                                     startNumberParam,
                                     state.HlsListSize.ToString(CultureInfo.InvariantCulture),
                                     baseUrlParam,
                                     outputPath
                                     ).Trim();

            return(args);
        }
 public DBCSDecoder(EncodingHelper encoding)
 {
     _encoding = encoding;
 }
Esempio n. 10
0
        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>StreamState.</returns>
        protected async Task <StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
        {
            ParseDlnaHeaders(request);

            if (!string.IsNullOrWhiteSpace(request.Params))
            {
                ParseParams(request);
            }

            var url = Request.PathInfo;

            if (string.IsNullOrEmpty(request.AudioCodec))
            {
                request.AudioCodec = EncodingHelper.InferAudioCodec(url);
            }

            var state = new StreamState(MediaSourceManager, Logger, TranscodingJobType)
            {
                Request      = request,
                RequestedUrl = url,
                UserAgent    = Request.UserAgent
            };

            var auth = AuthorizationContext.GetAuthorizationInfo(Request);

            if (!string.IsNullOrWhiteSpace(auth.UserId))
            {
                state.User = UserManager.GetUserById(auth.UserId);
            }

            //if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
            //    (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
            //    (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
            //{
            //    state.SegmentLength = 6;
            //}

            if (state.VideoRequest != null)
            {
                if (!string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec))
                {
                    state.SupportedVideoCodecs    = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
                    state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
                }
            }

            if (!string.IsNullOrWhiteSpace(request.AudioCodec))
            {
                state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
                state.Request.AudioCodec   = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i))
                                             ?? state.SupportedAudioCodecs.FirstOrDefault();
            }

            if (!string.IsNullOrWhiteSpace(request.SubtitleCodec))
            {
                state.SupportedSubtitleCodecs = request.SubtitleCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
                state.Request.SubtitleCodec   = state.SupportedSubtitleCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToSubtitleCodec(i))
                                                ?? state.SupportedSubtitleCodecs.FirstOrDefault();
            }

            var item = LibraryManager.GetItemById(request.Id);

            state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);

            MediaSourceInfo mediaSource = null;

            if (string.IsNullOrWhiteSpace(request.LiveStreamId))
            {
                TranscodingJob currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ?
                                            ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId)
                    : null;

                if (currentJob != null)
                {
                    mediaSource = currentJob.MediaSource;
                }

                if (mediaSource == null)
                {
                    var mediaSources = (await MediaSourceManager.GetPlayackMediaSources(request.Id, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false)).ToList();

                    mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
                       ? mediaSources.First()
                       : mediaSources.FirstOrDefault(i => string.Equals(i.Id, request.MediaSourceId));

                    if (mediaSource == null && string.Equals(request.Id, request.MediaSourceId, StringComparison.OrdinalIgnoreCase))
                    {
                        mediaSource = mediaSources.First();
                    }
                }
            }
            else
            {
                var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false);

                mediaSource = liveStreamInfo.Item1;
                state.DirectStreamProvider = liveStreamInfo.Item2;
            }

            var videoRequest = request as VideoStreamRequest;

            EncodingHelper.AttachMediaSourceInfo(state, mediaSource, url);

            var container = Path.GetExtension(state.RequestedUrl);

            if (string.IsNullOrEmpty(container))
            {
                container = request.Static ?
                            state.InputContainer :
                            (Path.GetExtension(GetOutputFilePath(state)) ?? string.Empty).TrimStart('.');
            }

            state.OutputContainer = (container ?? string.Empty).TrimStart('.');

            state.OutputAudioBitrate    = EncodingHelper.GetAudioBitrateParam(state.Request, state.AudioStream);
            state.OutputAudioSampleRate = request.AudioSampleRate;

            state.OutputAudioCodec = state.Request.AudioCodec;

            state.OutputAudioChannels = EncodingHelper.GetNumAudioChannelsParam(state.Request, state.AudioStream, state.OutputAudioCodec);

            if (videoRequest != null)
            {
                state.OutputVideoCodec   = state.VideoRequest.VideoCodec;
                state.OutputVideoBitrate = EncodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);

                if (videoRequest != null)
                {
                    EncodingHelper.TryStreamCopy(state);
                }

                if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
                {
                    var resolution = ResolutionNormalizer.Normalize(
                        state.VideoStream == null ? (int?)null : state.VideoStream.BitRate,
                        state.OutputVideoBitrate.Value,
                        state.VideoStream == null ? null : state.VideoStream.Codec,
                        state.OutputVideoCodec,
                        videoRequest.MaxWidth,
                        videoRequest.MaxHeight);

                    videoRequest.MaxWidth  = resolution.MaxWidth;
                    videoRequest.MaxHeight = resolution.MaxHeight;
                }

                ApplyDeviceProfileSettings(state);
            }
            else
            {
                ApplyDeviceProfileSettings(state);
            }

            state.OutputFilePath = GetOutputFilePath(state);

            return(state);
        }
Esempio n. 11
0
        /// <summary>
        /// Gets the video arguments.
        /// </summary>
        protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions)
        {
            if (!state.IsOutputVideo)
            {
                return(string.Empty);
            }

            var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions);

            var args = "-codec:v:0 " + codec;

            // if (state.EnableMpegtsM2TsMode)
            // {
            //     args += " -mpegts_m2ts_mode 1";
            // }

            // See if we can save come cpu cycles by avoiding encoding
            if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
            {
                // if h264_mp4toannexb is ever added, do not use it for live tv
                if (state.VideoStream != null &&
                    !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
                {
                    string bitStreamArgs = EncodingHelper.GetBitStreamArgs(state.VideoStream);
                    if (!string.IsNullOrEmpty(bitStreamArgs))
                    {
                        args += " " + bitStreamArgs;
                    }
                }
            }
            else
            {
                var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
                                                state.SegmentLength.ToString(CultureInfo.InvariantCulture));

                var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;

                args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultEncoderPreset()) + keyFrameArg;

                // Add resolution params, if specified
                if (!hasGraphicalSubs)
                {
                    args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec);
                }

                // This is for internal graphical subs
                if (hasGraphicalSubs)
                {
                    args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec);
                }
            }

            args += " -flags -global_header";

            if (!string.IsNullOrEmpty(state.OutputVideoSync))
            {
                args += " -vsync " + state.OutputVideoSync;
            }

            args += EncodingHelper.GetOutputFFlags(state);

            return(args);
        }
        public static async Task Execute(ISession session, WebSocketSession webSocketSession, string requestID)
        {
            var allItems = await HumanWalkSnipeTask.GetCurrentQueueItems(session);

            webSocketSession.Send(EncodingHelper.Serialize(new SnipeListResponce(allItems, requestID)));
        }
Esempio n. 13
0
        public async Task <ActionResult> GetVideoStream(
            [FromRoute] Guid itemId,
            [FromRoute] string?container,
            [FromQuery] bool? @static,
            [FromQuery] string? @params,
            [FromQuery] string?tag,
            [FromQuery] string?deviceProfileId,
            [FromQuery] string?playSessionId,
            [FromQuery] string?segmentContainer,
            [FromQuery] int?segmentLength,
            [FromQuery] int?minSegments,
            [FromQuery] string?mediaSourceId,
            [FromQuery] string?deviceId,
            [FromQuery] string?audioCodec,
            [FromQuery] bool?enableAutoStreamCopy,
            [FromQuery] bool?allowVideoStreamCopy,
            [FromQuery] bool?allowAudioStreamCopy,
            [FromQuery] bool?breakOnNonKeyFrames,
            [FromQuery] int?audioSampleRate,
            [FromQuery] int?maxAudioBitDepth,
            [FromQuery] int?audioBitRate,
            [FromQuery] int?audioChannels,
            [FromQuery] int?maxAudioChannels,
            [FromQuery] string?profile,
            [FromQuery] string?level,
            [FromQuery] float?framerate,
            [FromQuery] float?maxFramerate,
            [FromQuery] bool?copyTimestamps,
            [FromQuery] long?startTimeTicks,
            [FromQuery] int?width,
            [FromQuery] int?height,
            [FromQuery] int?videoBitRate,
            [FromQuery] int?subtitleStreamIndex,
            [FromQuery] SubtitleDeliveryMethod subtitleMethod,
            [FromQuery] int?maxRefFrames,
            [FromQuery] int?maxVideoBitDepth,
            [FromQuery] bool?requireAvc,
            [FromQuery] bool?deInterlace,
            [FromQuery] bool?requireNonAnamorphic,
            [FromQuery] int?transcodingMaxAudioChannels,
            [FromQuery] int?cpuCoreLimit,
            [FromQuery] string?liveStreamId,
            [FromQuery] bool?enableMpegtsM2TsMode,
            [FromQuery] string?videoCodec,
            [FromQuery] string?subtitleCodec,
            [FromQuery] string?transcodingReasons,
            [FromQuery] int?audioStreamIndex,
            [FromQuery] int?videoStreamIndex,
            [FromQuery] EncodingContext context,
            [FromQuery] Dictionary <string, string> streamOptions)
        {
            var isHeadRequest           = Request.Method == System.Net.WebRequestMethods.Http.Head;
            var cancellationTokenSource = new CancellationTokenSource();
            var streamingRequest        = new VideoRequestDto
            {
                Id                          = itemId,
                Container                   = container,
                Static                      = @static ?? true,
                Params                      = @params,
                Tag                         = tag,
                DeviceProfileId             = deviceProfileId,
                PlaySessionId               = playSessionId,
                SegmentContainer            = segmentContainer,
                SegmentLength               = segmentLength,
                MinSegments                 = minSegments,
                MediaSourceId               = mediaSourceId,
                DeviceId                    = deviceId,
                AudioCodec                  = audioCodec,
                EnableAutoStreamCopy        = enableAutoStreamCopy ?? true,
                AllowAudioStreamCopy        = allowAudioStreamCopy ?? true,
                AllowVideoStreamCopy        = allowVideoStreamCopy ?? true,
                BreakOnNonKeyFrames         = breakOnNonKeyFrames ?? false,
                AudioSampleRate             = audioSampleRate,
                MaxAudioChannels            = maxAudioChannels,
                AudioBitRate                = audioBitRate,
                MaxAudioBitDepth            = maxAudioBitDepth,
                AudioChannels               = audioChannels,
                Profile                     = profile,
                Level                       = level,
                Framerate                   = framerate,
                MaxFramerate                = maxFramerate,
                CopyTimestamps              = copyTimestamps ?? true,
                StartTimeTicks              = startTimeTicks,
                Width                       = width,
                Height                      = height,
                VideoBitRate                = videoBitRate,
                SubtitleStreamIndex         = subtitleStreamIndex,
                SubtitleMethod              = subtitleMethod,
                MaxRefFrames                = maxRefFrames,
                MaxVideoBitDepth            = maxVideoBitDepth,
                RequireAvc                  = requireAvc ?? true,
                DeInterlace                 = deInterlace ?? true,
                RequireNonAnamorphic        = requireNonAnamorphic ?? true,
                TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
                CpuCoreLimit                = cpuCoreLimit,
                LiveStreamId                = liveStreamId,
                EnableMpegtsM2TsMode        = enableMpegtsM2TsMode ?? true,
                VideoCodec                  = videoCodec,
                SubtitleCodec               = subtitleCodec,
                TranscodeReasons            = transcodingReasons,
                AudioStreamIndex            = audioStreamIndex,
                VideoStreamIndex            = videoStreamIndex,
                Context                     = context,
                StreamOptions               = streamOptions
            };

            using var state = await StreamingHelpers.GetStreamingState(
                      streamingRequest,
                      Request,
                      _authContext,
                      _mediaSourceManager,
                      _userManager,
                      _libraryManager,
                      _serverConfigurationManager,
                      _mediaEncoder,
                      _fileSystem,
                      _subtitleEncoder,
                      _configuration,
                      _dlnaManager,
                      _deviceManager,
                      _transcodingJobHelper,
                      _transcodingJobType,
                      cancellationTokenSource.Token)
                              .ConfigureAwait(false);

            if (@static.HasValue && @static.Value && state.DirectStreamProvider != null)
            {
                StreamingHelpers.AddDlnaHeaders(state, Response.Headers, true, startTimeTicks, Request, _dlnaManager);

                await new ProgressiveFileCopier(state.DirectStreamProvider, null, _transcodingJobHelper, CancellationToken.None)
                {
                    AllowEndOfFile = false
                }.WriteToAsync(Response.Body, CancellationToken.None)
                .ConfigureAwait(false);

                // TODO (moved from MediaBrowser.Api): Don't hardcode contentType
                return(File(Response.Body, MimeTypes.GetMimeType("file.ts") !));
            }

            // Static remote stream
            if (@static.HasValue && @static.Value && state.InputProtocol == MediaProtocol.Http)
            {
                StreamingHelpers.AddDlnaHeaders(state, Response.Headers, true, startTimeTicks, Request, _dlnaManager);

                var httpClient = _httpClientFactory.CreateClient();
                return(await FileStreamResponseHelpers.GetStaticRemoteStreamResult(state, isHeadRequest, httpClient, HttpContext).ConfigureAwait(false));
            }

            if (@static.HasValue && @static.Value && state.InputProtocol != MediaProtocol.File)
            {
                return(BadRequest($"Input protocol {state.InputProtocol} cannot be streamed statically"));
            }

            var outputPath       = state.OutputFilePath;
            var outputPathExists = System.IO.File.Exists(outputPath);

            var transcodingJob    = _transcodingJobHelper.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
            var isTranscodeCached = outputPathExists && transcodingJob != null;

            StreamingHelpers.AddDlnaHeaders(state, Response.Headers, (@static.HasValue && @static.Value) || isTranscodeCached, startTimeTicks, Request, _dlnaManager);

            // Static stream
            if (@static.HasValue && @static.Value)
            {
                var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath);

                if (state.MediaSource.IsInfiniteStream)
                {
                    await new ProgressiveFileCopier(state.MediaPath, null, _transcodingJobHelper, CancellationToken.None)
                    {
                        AllowEndOfFile = false
                    }.WriteToAsync(Response.Body, CancellationToken.None)
                    .ConfigureAwait(false);

                    return(File(Response.Body, contentType));
                }

                return(FileStreamResponseHelpers.GetStaticFileResult(
                           state.MediaPath,
                           contentType,
                           isHeadRequest,
                           HttpContext));
            }

            // Need to start ffmpeg (because media can't be returned directly)
            var encodingOptions            = _serverConfigurationManager.GetEncodingOptions();
            var encodingHelper             = new EncodingHelper(_mediaEncoder, _fileSystem, _subtitleEncoder, _configuration);
            var ffmpegCommandLineArguments = encodingHelper.GetProgressiveVideoFullCommandLine(state, encodingOptions, outputPath, "superfast");

            return(await FileStreamResponseHelpers.GetTranscodedFile(
                       state,
                       isHeadRequest,
                       HttpContext,
                       _transcodingJobHelper,
                       ffmpegCommandLineArguments,
                       _transcodingJobType,
                       cancellationTokenSource).ConfigureAwait(false));
        }
Esempio n. 14
0
        public void FileCreateBlank()
        {
            EngineState s       = EngineTests.CreateEngineState();
            string      destDir = FileHelper.GetTempDir();

            try
            {
                void Template(string rawCode, string fileName, Encoding encoding, bool createDummy, ErrorCheck check = ErrorCheck.Success)
                {
                    string destFullPath = Path.Combine(destDir, fileName);

                    if (File.Exists(destFullPath))
                    {
                        File.Delete(destFullPath);
                    }
                    try
                    {
                        if (createDummy)
                        {
                            File.Create(destFullPath).Close();
                        }

                        EngineTests.Eval(s, rawCode, CodeType.FileCreateBlank, check);

                        if (check == ErrorCheck.Success)
                        {
                            Assert.IsTrue(File.Exists(destFullPath));
                            Assert.IsTrue(EncodingHelper.DetectEncoding(destFullPath).Equals(encoding));
                            switch (encoding)
                            {
                            case UTF8Encoding utf8Enc:
                            {
                                byte[] preamble = utf8Enc.GetPreamble();
                                Assert.AreEqual(3, preamble.Length);
                                Assert.IsTrue(preamble.SequenceEqual(Encoding.UTF8.GetPreamble()));
                            }
                            break;

                            case UnicodeEncoding uniEnc:
                            {
                                byte[] preamble = uniEnc.GetPreamble();
                                Assert.AreEqual(2, preamble.Length);
                                if (encoding.Equals(Encoding.Unicode))
                                {
                                    Assert.IsTrue(preamble.SequenceEqual(Encoding.Unicode.GetPreamble()));
                                }
                                else if (encoding.Equals(Encoding.BigEndianUnicode))
                                {
                                    Assert.IsTrue(preamble.SequenceEqual(Encoding.BigEndianUnicode.GetPreamble()));
                                }
                                else
                                {
                                    Assert.Fail();
                                }
                            }
                            break;
                            }
                        }
                    }
                    finally
                    {
                        if (File.Exists(destFullPath))
                        {
                            File.Delete(destFullPath);
                        }
                    }
                }

                Template($@"FileCreateBlank,{destDir}\A.txt", "A.txt", EncodingHelper.DefaultAnsi, false);
                Template($@"FileCreateBlank,{destDir}\A.txt,UTF8", "A.txt", Encoding.UTF8, false);
                Template($@"FileCreateBlank,{destDir}\A.txt,UTF16", "A.txt", Encoding.Unicode, false);
                Template($@"FileCreateBlank,{destDir}\A.txt,UTF16BE", "A.txt", Encoding.BigEndianUnicode, false);
                Template($@"FileCreateBlank,{destDir}\A.txt", "A.txt", EncodingHelper.DefaultAnsi, true, ErrorCheck.Overwrite);
                Template($@"FileCreateBlank,{destDir}\A.txt,PRESERVE", "A.txt", EncodingHelper.DefaultAnsi, true, ErrorCheck.Overwrite);
                Template($@"FileCreateBlank,{destDir}\A.txt,PRESERVE", "A.txt", EncodingHelper.DefaultAnsi, false);
                Template($@"FileCreateBlank,{destDir}\A.txt,NOWARN", "A.txt", EncodingHelper.DefaultAnsi, true);
                Template($@"FileCreateBlank,{destDir}\A.txt,NOWARN", "A.txt", EncodingHelper.DefaultAnsi, false);
                Template($@"FileCreateBlank,{destDir}\A.txt,PRESERVE,NOWARN", "A.txt", EncodingHelper.DefaultAnsi, true);
                Template($@"FileCreateBlank,{destDir}\A.txt,PRESERVE,NOWARN", "A.txt", EncodingHelper.DefaultAnsi, false);
            }
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir);
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Starts FFmpeg.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="outputPath">The output path.</param>
        /// <param name="commandLineArguments">The command line arguments for FFmpeg.</param>
        /// <param name="request">The <see cref="HttpRequest"/>.</param>
        /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
        /// <param name="cancellationTokenSource">The cancellation token source.</param>
        /// <param name="workingDirectory">The working directory.</param>
        /// <returns>Task.</returns>
        public async Task <TranscodingJobDto> StartFfMpeg(
            StreamState state,
            string outputPath,
            string commandLineArguments,
            HttpRequest request,
            TranscodingJobType transcodingJobType,
            CancellationTokenSource cancellationTokenSource,
            string?workingDirectory = null)
        {
            var directory = Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({outputPath}) is not valid.", nameof(outputPath));

            Directory.CreateDirectory(directory);

            await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);

            if (state.VideoRequest != null && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
            {
                var auth = await _authorizationContext.GetAuthorizationInfo(request).ConfigureAwait(false);

                if (auth.User != null && !auth.User.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding))
                {
                    this.OnTranscodeFailedToStart(outputPath, transcodingJobType, state);

                    throw new ArgumentException("User does not have access to video transcoding.");
                }
            }

            if (string.IsNullOrEmpty(_mediaEncoder.EncoderPath))
            {
                throw new ArgumentException("FFmpeg path not set.");
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    WindowStyle     = ProcessWindowStyle.Hidden,
                    CreateNoWindow  = true,
                    UseShellExecute = false,

                    // Must consume both stdout and stderr or deadlocks may occur
                    // RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    FileName         = _mediaEncoder.EncoderPath,
                    Arguments        = commandLineArguments,
                    WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? string.Empty : workingDirectory,
                    ErrorDialog      = false
                },
                EnableRaisingEvents = true
            };

            var transcodingJob = this.OnTranscodeBeginning(
                outputPath,
                state.Request.PlaySessionId,
                state.MediaSource.LiveStreamId,
                Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
                transcodingJobType,
                process,
                state.Request.DeviceId,
                state,
                cancellationTokenSource);

            _logger.LogInformation("{Filename} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments);

            var logFilePrefix = "FFmpeg.Transcode-";

            if (state.VideoRequest != null &&
                EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
            {
                logFilePrefix = EncodingHelper.IsCopyCodec(state.OutputAudioCodec)
                    ? "FFmpeg.Remux-"
                    : "FFmpeg.DirectStream-";
            }

            var logFilePath = Path.Combine(
                _serverConfigurationManager.ApplicationPaths.LogDirectoryPath,
                $"{logFilePrefix}{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{state.Request.MediaSourceId}_{Guid.NewGuid().ToString()[..8]}.log");
Esempio n. 16
0
        internal XlsBiffFont(byte[] bytes, uint offset, int biffVersion)
            : base(bytes, offset)
        {
            if (Id == BIFFRECORDTYPE.FONT_V34)
            {
                _fontName = new XlsShortByteString(bytes, offset + 4 + 6);
            }
            else if (Id == BIFFRECORDTYPE.FONT && biffVersion == 2)
            {
                _fontName = new XlsShortByteString(bytes, offset + 4 + 4);
            }
            else if (Id == BIFFRECORDTYPE.FONT && biffVersion == 5)
            {
                _fontName = new XlsShortByteString(bytes, offset + 4 + 14);
            }
            else if (Id == BIFFRECORDTYPE.FONT && biffVersion == 8)
            {
                _fontName = new XlsShortUnicodeString(bytes, offset + 4 + 14);
            }
            else
            {
                _fontName = new XlsInternalString(string.Empty);
            }

            if (Id == BIFFRECORDTYPE.FONT && biffVersion >= 5)
            {
                // Encodings were mapped by correlating this:
                // https://docs.microsoft.com/en-us/windows/desktop/intl/code-page-identifiers
                // with the FONT record character set table here:
                // https://www.openoffice.org/sc/excelfileformat.pdf
                var byteStringCharacterSet = ReadByte(12);
                switch (byteStringCharacterSet)
                {
                case 0:     // ANSI Latin
                case 1:     // System default
                    ByteStringEncoding = EncodingHelper.GetEncoding(1252);
                    break;

                case 77:     // Apple roman
                    ByteStringEncoding = EncodingHelper.GetEncoding(10000);
                    break;

                case 128:     // ANSI Japanese Shift-JIS
                    ByteStringEncoding = EncodingHelper.GetEncoding(932);
                    break;

                case 129:     // ANSI Korean (Hangul)
                    ByteStringEncoding = EncodingHelper.GetEncoding(949);
                    break;

                case 130:     // ANSI Korean (Johab)
                    ByteStringEncoding = EncodingHelper.GetEncoding(1361);
                    break;

                case 134:     // ANSI Chinese Simplified GBK
                    ByteStringEncoding = EncodingHelper.GetEncoding(936);
                    break;

                case 136:     // ANSI Chinese Traditional BIG5
                    ByteStringEncoding = EncodingHelper.GetEncoding(950);
                    break;

                case 161:     // ANSI Greek
                    ByteStringEncoding = EncodingHelper.GetEncoding(1253);
                    break;

                case 162:     // ANSI Turkish
                    ByteStringEncoding = EncodingHelper.GetEncoding(1254);
                    break;

                case 163:     // ANSI Vietnamese
                    ByteStringEncoding = EncodingHelper.GetEncoding(1258);
                    break;

                case 177:     // ANSI Hebrew
                    ByteStringEncoding = EncodingHelper.GetEncoding(1255);
                    break;

                case 178:     // ANSI Arabic
                    ByteStringEncoding = EncodingHelper.GetEncoding(1256);
                    break;

                case 186:     // ANSI Baltic
                    ByteStringEncoding = EncodingHelper.GetEncoding(1257);
                    break;

                case 204:     // ANSI Cyrillic
                    ByteStringEncoding = EncodingHelper.GetEncoding(1251);
                    break;

                case 222:     // ANSI Thai
                    ByteStringEncoding = EncodingHelper.GetEncoding(874);
                    break;

                case 238:     // ANSI Latin II
                    ByteStringEncoding = EncodingHelper.GetEncoding(1250);
                    break;

                case 255:     // OEM Latin
                    ByteStringEncoding = EncodingHelper.GetEncoding(850);
                    break;
                }
            }
        }
Esempio n. 17
0
        public static async Task Execute(ISession session, WebSocketSession webSocketSession, string requestID)
        {
            var allItems = await session.Inventory.GetItems();

            webSocketSession.Send(EncodingHelper.Serialize(new ItemListResponce(allItems, requestID)));
        }
Esempio n. 18
0
        protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions)
        {
            if (!state.IsOutputVideo)
            {
                return(string.Empty);
            }

            var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions);

            var args = "-codec:v:0 " + codec;

            // if (state.EnableMpegtsM2TsMode)
            // {
            //     args += " -mpegts_m2ts_mode 1";
            // }

            // See if we can save come cpu cycles by avoiding encoding
            if (EncodingHelper.IsCopyCodec(codec))
            {
                if (state.VideoStream != null && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
                {
                    string bitStreamArgs = EncodingHelper.GetBitStreamArgs(state.VideoStream);
                    if (!string.IsNullOrEmpty(bitStreamArgs))
                    {
                        args += " " + bitStreamArgs;
                    }
                }

                //args += " -flags -global_header";
            }
            else
            {
                var gopArg      = string.Empty;
                var keyFrameArg = string.Format(
                    CultureInfo.InvariantCulture,
                    " -force_key_frames:0 \"expr:gte(t,{0}+n_forced*{1})\"",
                    GetStartNumber(state) * state.SegmentLength,
                    state.SegmentLength);

                var framerate = state.VideoStream?.RealFrameRate;

                if (framerate.HasValue)
                {
                    // This is to make sure keyframe interval is limited to our segment,
                    // as forcing keyframes is not enough.
                    // Example: we encoded half of desired length, then codec detected
                    // scene cut and inserted a keyframe; next forced keyframe would
                    // be created outside of segment, which breaks seeking
                    // -sc_threshold 0 is used to prevent the hardware encoder from post processing to break the set keyframe
                    gopArg = string.Format(
                        CultureInfo.InvariantCulture,
                        " -g {0} -keyint_min {0} -sc_threshold 0",
                        Math.Ceiling(state.SegmentLength * framerate.Value)
                        );
                }

                args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultEncoderPreset());

                // Unable to force key frames using these hw encoders, set key frames by GOP
                if (string.Equals(codec, "h264_qsv", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(codec, "h264_nvenc", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(codec, "h264_amf", StringComparison.OrdinalIgnoreCase))
                {
                    args += " " + gopArg;
                }
                else
                {
                    args += " " + keyFrameArg + gopArg;
                }

                //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";

                var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;

                // This is for graphical subs
                if (hasGraphicalSubs)
                {
                    args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec);
                }
                // Add resolution params, if specified
                else
                {
                    args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec);
                }

                // -start_at_zero is necessary to use with -ss when seeking,
                // otherwise the target position cannot be determined.
                if (!(state.SubtitleStream != null && state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream))
                {
                    args += " -start_at_zero";
                }

                //args += " -flags -global_header";
            }

            if (!string.IsNullOrEmpty(state.OutputVideoSync))
            {
                args += " -vsync " + state.OutputVideoSync;
            }

            args += EncodingHelper.GetOutputFFlags(state);

            return(args);
        }
Esempio n. 19
0
        public static List <LogInfo> TXTAddLineOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            CodeInfo_TXTAddLineOp infoOp = cmd.Info.Cast <CodeInfo_TXTAddLineOp>();

            CodeInfo_TXTAddLine firstInfo = infoOp.Infos[0];
            string         fileName       = StringEscaper.Preprocess(s, firstInfo.FileName);
            string         modeStr        = StringEscaper.Preprocess(s, firstInfo.Mode);
            TXTAddLineMode mode;

            if (modeStr.Equals("Append", StringComparison.OrdinalIgnoreCase))
            {
                mode = TXTAddLineMode.Append;
            }
            else if (modeStr.Equals("Prepend", StringComparison.OrdinalIgnoreCase))
            {
                mode = TXTAddLineMode.Prepend;
            }
            else
            {
                throw new ExecuteException($"Mode [{modeStr}] must be one of [Append, Prepend]");
            }

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            // Detect encoding of text. If text does not exists, create blank file (ANSI)
            Encoding encoding;

            if (File.Exists(fileName))
            {
                encoding = EncodingHelper.SmartDetectEncoding(fileName, infoOp.Infos.Select(x => x.Line));
            }
            else
            {
                encoding = EncodingHelper.DefaultAnsi;
            }

            string linesToWrite;

            if (mode == TXTAddLineMode.Prepend)
            {
                string tempPath = FileHelper.GetTempFile();
                using (StreamReader r = new StreamReader(fileName, encoding, false))
                    using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
                    {
                        StringBuilder b = new StringBuilder();
                        List <CodeInfo_TXTAddLine> infos = infoOp.Infos;
                        infos.Reverse();
                        foreach (CodeInfo_TXTAddLine subInfo in infos)
                        {
                            b.AppendLine(StringEscaper.Preprocess(s, subInfo.Line));
                        }
                        linesToWrite = b.ToString();

                        w.Write(linesToWrite);
                        w.Write(r.ReadToEnd());
                    }
                FileHelper.FileReplaceEx(tempPath, fileName);

                logs.Add(new LogInfo(LogState.Success, $"Lines prepended to [{fileName}] : \r\n{linesToWrite}", cmd));
            }
            else if (mode == TXTAddLineMode.Append)
            {
                StringBuilder b = new StringBuilder();
                foreach (CodeInfo_TXTAddLine subInfo in infoOp.Infos)
                {
                    b.AppendLine(StringEscaper.Preprocess(s, subInfo.Line));
                }

                linesToWrite = b.ToString();

                bool newLineExist = true;
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        long   bomLen   = EncodingHelper.TextBomLength(fs);
                        byte[] lastChar = new byte[2];
                        if (2 + bomLen <= fs.Length)
                        {
                            fs.Position = fs.Length - 2;
                            fs.Read(lastChar, 0, 2);
                            if (lastChar[0] != '\r' || lastChar[1] != '\n')
                            {
                                newLineExist = false;
                            }
                        }
                    }
                }

                if (newLineExist)
                {
                    File.AppendAllText(fileName, linesToWrite, encoding);
                }
                else
                {
                    File.AppendAllText(fileName, "\r\n" + linesToWrite, encoding);
                }

                logs.Add(new LogInfo(LogState.Success, $"Lines appended to [{fileName}] : \r\n{linesToWrite}", cmd));
            }
            else
            {
                throw new InvalidOperationException("Internal Logic Error at TXTAddLine");
            }

            return(logs);
        }
Esempio n. 20
0
        private void SaveXml(string path, bool includeImage)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Encoding = Encoding.UTF8;
            settings.Indent   = true;

            using (Stream stream = File.Create(path))
            {
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    writer.WriteStartDocument();

                    writer.WriteStartElement("TextureFont");

                    if (includeImage)
                    {
                        writer.WriteStartElement("Texture");
                        writer.WriteElementString("Width", _fontTexture.Texture.Width.ToString());
                        writer.WriteElementString("Height", _fontTexture.Texture.Height.ToString());
                        writer.WriteElementString("IsCompressed", chbCompressImage.Checked.ToString());

                        byte[] imageData = null;
                        using (var mem = new MemoryStream())
                        {
                            _fontTexture.Texture.Save(mem, ImageFormat.Png);
                            imageData = mem.ToArray();
                        }

                        if (chbCompressImage.Checked)
                        {
                            imageData = CompressHelper.GZIPCompress(imageData);
                        }

                        var image = EncodingHelper.EncodeBase64(imageData, null);

                        writer.WriteElementString("Data", image);
                        writer.WriteEndElement();
                    }
                    else
                    {
                        string pathImage = Path.Combine(
                            Path.GetDirectoryName(path),
                            Path.GetFileNameWithoutExtension(path) + ".png");

                        _fontTexture.Texture.Save(pathImage, ImageFormat.Png);
                    }

                    writer.WriteStartElement("Characters");

                    writer.WriteStartElement("FirstChar");
                    writer.WriteAttributeString("C", _fontTexture.FirstChar.ToString());
                    writer.WriteAttributeString("A", ((int)_fontTexture.FirstChar).ToString());
                    writer.WriteEndElement();

                    writer.WriteStartElement("Chars");
                    for (int i = 0; i < _fontTexture.Positions.Length; i++)
                    {
                        writer.WriteStartElement("Char");
                        writer.WriteAttributeString("X", _fontTexture.Positions[i].X.ToString());
                        writer.WriteAttributeString("Y", _fontTexture.Positions[i].Y.ToString());
                        writer.WriteAttributeString("W", _fontTexture.Positions[i].Width.ToString());
                        writer.WriteAttributeString("H", _fontTexture.Positions[i].Height.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // characters
                    writer.WriteEndElement(); // texture font

                    writer.WriteEndDocument();
                }
            }
        }
Esempio n. 21
0
        public static List <LogInfo> TXTAddLine(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_TXTAddLine info = cmd.Info.Cast <CodeInfo_TXTAddLine>();

            string         fileName = StringEscaper.Preprocess(s, info.FileName);
            string         line     = StringEscaper.Preprocess(s, info.Line);
            string         modeStr  = StringEscaper.Preprocess(s, info.Mode);
            TXTAddLineMode mode;

            if (modeStr.Equals("Append", StringComparison.OrdinalIgnoreCase))
            {
                mode = TXTAddLineMode.Append;
            }
            else if (info.Mode.Equals("Prepend", StringComparison.OrdinalIgnoreCase))
            {
                mode = TXTAddLineMode.Prepend;
            }
            else
            {
                throw new ExecuteException($"Mode [{modeStr}] must be one of [Append, Prepend]");
            }

            if (!StringEscaper.PathSecurityCheck(fileName, out string errorMsg))
            {
                return(LogInfo.LogErrorMessage(logs, errorMsg));
            }

            // Detect encoding of text. If text does not exists, create blank file (ANSI)
            Encoding encoding;

            if (File.Exists(fileName))
            {
                encoding = EncodingHelper.SmartDetectEncoding(fileName, line);
            }
            else
            {
                encoding = EncodingHelper.DefaultAnsi;
            }

            if (mode == TXTAddLineMode.Prepend)
            {
                string tempPath = FileHelper.GetTempFile();
                using (StreamReader r = new StreamReader(fileName, encoding, false))
                    using (StreamWriter w = new StreamWriter(tempPath, false, encoding))
                    {
                        w.WriteLine(line);
                        string lineFromSrc;
                        while ((lineFromSrc = r.ReadLine()) != null)
                        {
                            w.WriteLine(lineFromSrc);
                        }
                    }
                FileHelper.FileReplaceEx(tempPath, fileName);

                logs.Add(new LogInfo(LogState.Success, $"Prepended [{line}] to [{fileName}]", cmd));
            }
            else if (mode == TXTAddLineMode.Append)
            {
                bool newLineExist = true;
                if (File.Exists(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        long   bomLen   = EncodingHelper.TextBomLength(fs);
                        byte[] lastChar = new byte[2];
                        if (2 + bomLen <= fs.Length)
                        {
                            fs.Position = fs.Length - 2;
                            fs.Read(lastChar, 0, 2);
                            if (lastChar[0] != '\r' || lastChar[1] != '\n')
                            {
                                newLineExist = false;
                            }
                        }
                    }
                }

                if (newLineExist)
                {
                    File.AppendAllText(fileName, line + "\r\n", encoding);
                }
                else
                {
                    File.AppendAllText(fileName, "\r\n" + line + "\r\n", encoding);
                }
                logs.Add(new LogInfo(LogState.Success, $"Appended [{line}] to [{fileName}]", cmd));
            }
            else
            {
                throw new InvalidOperationException("Internal Logic Error at TXTAddLine");
            }

            return(logs);
        }
Esempio n. 22
0
 /// <summary>
 /// 将字节数组转为字符串
 /// </summary>
 /// <param name="b">字节数组</param>
 /// <param name="e">编码,默认为Default</param>
 /// <returns>字符串</returns>
 public static string ByteToString(byte[] b, Encoding e = null)
 {
     return(EncodingHelper.ByteToString(b, e));
 }
 public static string GenerateBase64EncodedRandomString(int len)
 {
     return(EncodingHelper.Base64Encode(GenerateRandomString(len)).Substring(0, len));
 }
Esempio n. 24
0
 /// <summary>
 /// 将字符串转为字节数组
 /// </summary>
 /// <param name="s">字符串</param>
 /// <param name="e">编码,默认为Default</param>
 /// <returns>字节数组</returns>
 public static byte[] StringToByte(string s, Encoding e = null)
 {
     return(EncodingHelper.StringToByte(s, e));
 }
Esempio n. 25
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            long value = Convert.ToInt64(reader.Value);

            return(EncodingHelper.DateTimeFromUnixTimestampMillis(value));
        }
Esempio n. 26
0
        protected override string GetAudioArguments(StreamState state, EncodingOptions encodingOptions)
        {
            var audioCodec = EncodingHelper.GetAudioEncoder(state);

            if (!state.IsOutputVideo)
            {
                if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase))
                {
                    return("-acodec copy");
                }

                var audioTranscodeParams = new List <string>();

                audioTranscodeParams.Add("-acodec " + audioCodec);

                if (state.OutputAudioBitrate.HasValue)
                {
                    audioTranscodeParams.Add("-ab " + state.OutputAudioBitrate.Value.ToString(UsCulture));
                }

                if (state.OutputAudioChannels.HasValue)
                {
                    audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(UsCulture));
                }

                if (state.OutputAudioSampleRate.HasValue)
                {
                    audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture));
                }

                audioTranscodeParams.Add("-vn");
                return(string.Join(" ", audioTranscodeParams.ToArray()));
            }

            if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase))
            {
                var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions);

                if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.EnableBreakOnNonKeyFrames(videoCodec))
                {
                    return("-codec:a:0 copy -copypriorss:a:0 0");
                }

                return("-codec:a:0 copy");
            }

            var args = "-codec:a:0 " + audioCodec;

            var channels = state.OutputAudioChannels;

            if (channels.HasValue)
            {
                args += " -ac " + channels.Value;
            }

            var bitrate = state.OutputAudioBitrate;

            if (bitrate.HasValue)
            {
                args += " -ab " + bitrate.Value.ToString(UsCulture);
            }

            if (state.OutputAudioSampleRate.HasValue)
            {
                args += " -ar " + state.OutputAudioSampleRate.Value.ToString(UsCulture);
            }

            args += " " + EncodingHelper.GetAudioFilterParam(state, encodingOptions, true);

            return(args);
        }
Esempio n. 27
0
        private static Encoding GetEncoding(int codePage)
        {
            Encoding enc = EncodingHelper.GetSupportedConsoleEncoding(codePage);

            return(new ConsoleEncoding(enc)); // ensure encoding doesn't output a preamble
        }
Esempio n. 28
0
        protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions)
        {
            if (!state.IsOutputVideo)
            {
                return(string.Empty);
            }

            var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions);

            var args = "-codec:v:0 " + codec;

            // if (state.EnableMpegtsM2TsMode)
            // {
            //     args += " -mpegts_m2ts_mode 1";
            // }

            // See if we can save come cpu cycles by avoiding encoding
            if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
            {
                if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
                {
                    args += " -bsf:v h264_mp4toannexb";
                }

                //args += " -flags -global_header";
            }
            else
            {
                var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
                                                state.SegmentLength.ToString(UsCulture));

                var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;

                args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg;

                //args += " -mixed-refs 0 -refs 3 -x264opts b_pyramid=0:weightb=0:weightp=0";

                // Add resolution params, if specified
                if (!hasGraphicalSubs)
                {
                    args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec, true);
                }

                // This is for internal graphical subs
                if (hasGraphicalSubs)
                {
                    args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec);
                }

                //args += " -flags -global_header";
            }

            if (args.IndexOf("-copyts", StringComparison.OrdinalIgnoreCase) == -1)
            {
                args += " -copyts";
            }

            if (!string.IsNullOrEmpty(state.OutputVideoSync))
            {
                args += " -vsync " + state.OutputVideoSync;
            }

            args += EncodingHelper.GetOutputFFlags(state);

            return(args);
        }
 /// <summary>
 ///     Initializes a new instance of the FieldEncoding class with a fixed encoding.
 /// </summary>
 /// <param name="encodingName">The field encoding name.</param>
 public FieldEncodingAttribute(string encodingName)
 {
     _encoding   = EncodingHelper.GetEncoding(encodingName);
     BindingMode = BindingMode.OneWay;
 }
Esempio n. 30
0
        private string GetCommandLineArgs(MediaSourceInfo mediaSource, string inputTempFile, string targetFile, TimeSpan duration)
        {
            string videoArgs;

            if (EncodeVideo(mediaSource))
            {
                const int MaxBitrate = 25000000;
                videoArgs = string.Format(
                    CultureInfo.InvariantCulture,
                    "-codec:v:0 libx264 -force_key_frames \"expr:gte(t,n_forced*5)\" {0} -pix_fmt yuv420p -preset superfast -crf 23 -b:v {1} -maxrate {1} -bufsize ({1}*2) -vsync -1 -profile:v high -level 41",
                    GetOutputSizeParam(),
                    MaxBitrate);
            }
            else
            {
                videoArgs = "-codec:v:0 copy";
            }

            videoArgs += " -fflags +genpts";

            var flags = new List <string>();

            if (mediaSource.IgnoreDts)
            {
                flags.Add("+igndts");
            }

            if (mediaSource.IgnoreIndex)
            {
                flags.Add("+ignidx");
            }

            if (mediaSource.GenPtsInput)
            {
                flags.Add("+genpts");
            }

            var inputModifier = "-async 1 -vsync -1";

            if (flags.Count > 0)
            {
                inputModifier += " -fflags " + string.Join(string.Empty, flags);
            }

            if (mediaSource.ReadAtNativeFramerate)
            {
                inputModifier += " -re";
            }

            if (mediaSource.RequiresLooping)
            {
                inputModifier += " -stream_loop -1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2";
            }

            var analyzeDurationSeconds = 5;
            var analyzeDuration        = " -analyzeduration " +
                                         (analyzeDurationSeconds * 1000000).ToString(CultureInfo.InvariantCulture);

            inputModifier += analyzeDuration;

            var subtitleArgs = CopySubtitles ? " -codec:s copy" : " -sn";

            // var outputParam = string.Equals(Path.GetExtension(targetFile), ".mp4", StringComparison.OrdinalIgnoreCase) ?
            //    " -f mp4 -movflags frag_keyframe+empty_moov" :
            //    string.Empty;

            var outputParam = string.Empty;

            var threads         = EncodingHelper.GetNumberOfThreads(null, _serverConfigurationManager.GetEncodingOptions(), null);
            var commandLineArgs = string.Format(
                CultureInfo.InvariantCulture,
                "-i \"{0}\" {2} -map_metadata -1 -threads {6} {3}{4}{5} -y \"{1}\"",
                inputTempFile,
                targetFile,
                videoArgs,
                GetAudioArgs(mediaSource),
                subtitleArgs,
                outputParam,
                threads);

            return(inputModifier + " " + commandLineArgs);
        }
Esempio n. 31
0
        public async Task ExtractVideoImagesOnInterval(string[] inputFiles,
                                                       string container,
                                                       MediaStream videoStream,
                                                       MediaProtocol protocol,
                                                       Video3DFormat?threedFormat,
                                                       TimeSpan interval,
                                                       string targetDirectory,
                                                       string filenamePrefix,
                                                       int?maxWidth,
                                                       CancellationToken cancellationToken)
        {
            var resourcePool = _thumbnailResourcePool;

            var inputArgument = GetInputArgument(inputFiles, protocol);

            var vf = "fps=fps=1/" + interval.TotalSeconds.ToString(UsCulture);

            if (maxWidth.HasValue)
            {
                var maxWidthParam = maxWidth.Value.ToString(UsCulture);

                vf += string.Format(",scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam);
            }

            Directory.CreateDirectory(targetDirectory);
            var outputPath = Path.Combine(targetDirectory, filenamePrefix + "%05d.jpg");

            var args = string.Format("-i {0} -threads 0 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf);

            var probeSizeArgument       = EncodingHelper.GetProbeSizeArgument(1);
            var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1);

            if (!string.IsNullOrWhiteSpace(probeSizeArgument))
            {
                args = probeSizeArgument + " " + args;
            }

            if (!string.IsNullOrWhiteSpace(analyzeDurationArgument))
            {
                args = analyzeDurationArgument + " " + args;
            }

            var encodinghelper = new EncodingHelper(this, FileSystem, SubtitleEncoder());

            if (videoStream != null)
            {
                /* fix
                 * var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions());
                 * if (!string.IsNullOrWhiteSpace(decoder))
                 * {
                 *  args = decoder + " " + args;
                 * }
                 */
            }

            if (!string.IsNullOrWhiteSpace(container))
            {
                var inputFormat = encodinghelper.GetInputFormat(container);
                if (!string.IsNullOrWhiteSpace(inputFormat))
                {
                    args = "-f " + inputFormat + " " + args;
                }
            }

            var process = _processFactory.Create(new ProcessOptions
            {
                CreateNoWindow      = true,
                UseShellExecute     = false,
                FileName            = FFmpegPath,
                Arguments           = args,
                IsHidden            = true,
                ErrorDialog         = false,
                EnableRaisingEvents = true
            });

            _logger.LogInformation(process.StartInfo.FileName + " " + process.StartInfo.Arguments);

            await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);

            bool ranToCompletion = false;

            using (var processWrapper = new ProcessWrapper(process, this, _logger))
            {
                try
                {
                    StartProcess(processWrapper);

                    // Need to give ffmpeg enough time to make all the thumbnails, which could be a while,
                    // but we still need to detect if the process hangs.
                    // Making the assumption that as long as new jpegs are showing up, everything is good.

                    bool isResponsive = true;
                    int  lastCount    = 0;

                    while (isResponsive)
                    {
                        if (await process.WaitForExitAsync(30000).ConfigureAwait(false))
                        {
                            ranToCompletion = true;
                            break;
                        }

                        cancellationToken.ThrowIfCancellationRequested();

                        var jpegCount = FileSystem.GetFilePaths(targetDirectory)
                                        .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase));

                        isResponsive = (jpegCount > lastCount);
                        lastCount    = jpegCount;
                    }

                    if (!ranToCompletion)
                    {
                        StopProcess(processWrapper, 1000);
                    }
                }
                finally
                {
                    resourcePool.Release();
                }

                var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;

                if (exitCode == -1)
                {
                    var msg = string.Format("ffmpeg image extraction failed for {0}", inputArgument);

                    _logger.LogError(msg);

                    throw new Exception(msg);
                }
            }
        }
Esempio n. 32
0
        /// <summary>
        /// Starts the FFMPEG.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="outputPath">The output path.</param>
        /// <param name="commandLineArguments">The command line arguments for ffmpeg.</param>
        /// <param name="request">The <see cref="HttpRequest"/>.</param>
        /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
        /// <param name="cancellationTokenSource">The cancellation token source.</param>
        /// <param name="workingDirectory">The working directory.</param>
        /// <returns>Task.</returns>
        public async Task <TranscodingJobDto> StartFfMpeg(
            StreamState state,
            string outputPath,
            string commandLineArguments,
            HttpRequest request,
            TranscodingJobType transcodingJobType,
            CancellationTokenSource cancellationTokenSource,
            string?workingDirectory = null)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

            await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);

            if (state.VideoRequest != null && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
            {
                var auth = _authorizationContext.GetAuthorizationInfo(request);
                if (auth.User != null && !auth.User.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding))
                {
                    this.OnTranscodeFailedToStart(outputPath, transcodingJobType, state);

                    throw new ArgumentException("User does not have access to video transcoding");
                }
            }

            if (string.IsNullOrEmpty(_mediaEncoder.EncoderPath))
            {
                throw new ArgumentException("FFMPEG path not set.");
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    WindowStyle     = ProcessWindowStyle.Hidden,
                    CreateNoWindow  = true,
                    UseShellExecute = false,

                    // Must consume both stdout and stderr or deadlocks may occur
                    // RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    FileName         = _mediaEncoder.EncoderPath,
                    Arguments        = commandLineArguments,
                    WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? null : workingDirectory,
                    ErrorDialog      = false
                },
                EnableRaisingEvents = true
            };

            var transcodingJob = this.OnTranscodeBeginning(
                outputPath,
                state.Request.PlaySessionId,
                state.MediaSource.LiveStreamId,
                Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
                transcodingJobType,
                process,
                state.Request.DeviceId,
                state,
                cancellationTokenSource);

            var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;

            _logger.LogInformation(commandLineLogMessage);

            var logFilePrefix = "ffmpeg-transcode";

            if (state.VideoRequest != null &&
                EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
            {
                logFilePrefix = EncodingHelper.IsCopyCodec(state.OutputAudioCodec)
                    ? "ffmpeg-remux"
                    : "ffmpeg-directstream";
            }

            var logFilePath = Path.Combine(_serverConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt");

            // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
            Stream logStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true);

            var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(request.Path + Environment.NewLine + Environment.NewLine + JsonSerializer.Serialize(state.MediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
            await logStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationTokenSource.Token).ConfigureAwait(false);

            process.Exited += (sender, args) => OnFfMpegProcessExited(process, transcodingJob, state);

            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error starting ffmpeg");

                this.OnTranscodeFailedToStart(outputPath, transcodingJobType, state);

                throw;
            }

            _logger.LogDebug("Launched ffmpeg process");
            state.TranscodingJob = transcodingJob;

            // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
            _ = new JobLogger(_logger).StartStreamingLog(state, process.StandardError.BaseStream, logStream);

            // Wait for the file to exist before proceeeding
            var ffmpegTargetFile = state.WaitForPath ?? outputPath;

            _logger.LogDebug("Waiting for the creation of {0}", ffmpegTargetFile);
            while (!File.Exists(ffmpegTargetFile) && !transcodingJob.HasExited)
            {
                await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            _logger.LogDebug("File {0} created or transcoding has finished", ffmpegTargetFile);

            if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited)
            {
                await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false);

                if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited)
                {
                    await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false);
                }
            }

            if (!transcodingJob.HasExited)
            {
                StartThrottler(state, transcodingJob);
            }

            _logger.LogDebug("StartFfMpeg() finished successfully");

            return(transcodingJob);
        }