コード例 #1
0
        public static int GetMpegStreamType(string path)
        {
            int mpegType = -1;

            using (FileStream fs = File.OpenRead(path))
            {
                // look for first packet
                long currentOffset = ParseFile.GetNextOffset(fs, 0, MpegStream.PacketStartBytes);

                if (currentOffset != -1)
                {
                    currentOffset += 4;
                    fs.Position    = currentOffset;
                    byte idByte = (byte)fs.ReadByte();

                    if ((int)ByteConversion.GetHighNibble(idByte) == 2)
                    {
                        mpegType = 1;
                    }
                    else if ((int)ByteConversion.GetHighNibble(idByte) == 4)
                    {
                        mpegType = 2;
                    }
                }
                else
                {
                    throw new FormatException(String.Format("Cannot find Pack Header for file: {0}{1}", Path.GetFileName(path), Environment.NewLine));
                }
            }

            return(mpegType);
        }
コード例 #2
0
        public static void RenameFileUsingInternalName(string path,
                                                       long offset, int length, byte[] terminatorBytes, bool maintainFileExtension)
        {
            string destinationDirectory = Path.GetDirectoryName(path);
            string destinationFile;
            string originalExtension;

            int nameLength;

            byte[] nameByteArray;

            using (FileStream fs = File.OpenRead(path))
            {
                if (terminatorBytes != null)
                {
                    nameLength = ParseFile.GetSegmentLength(fs, (int)offset, terminatorBytes);
                }
                else
                {
                    nameLength = length;
                }

                if (nameLength < 1)
                {
                    throw new ArgumentOutOfRangeException("Name Length", "Name Length is less than 1.");
                }

                if (maintainFileExtension)
                {
                    originalExtension = Path.GetExtension(path);
                }

                nameByteArray   = ParseFile.ParseSimpleOffset(fs, offset, nameLength);
                destinationFile = ByteConversion.GetAsciiText(FileUtil.ReplaceNullByteWithSpace(nameByteArray)).Trim();
                destinationFile = Path.Combine(destinationDirectory, destinationFile);

                if (maintainFileExtension)
                {
                    originalExtension = Path.GetExtension(path);
                    destinationFile   = Path.ChangeExtension(destinationFile, originalExtension);
                }
            }

            // try to copy using the new name
            if (!path.Equals(destinationFile))
            {
                try
                {
                    if (!Directory.Exists(Path.GetDirectoryName(destinationFile)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(destinationFile));
                    }

                    if (File.Exists(destinationFile))
                    {
                        string[] sameNamedFiles = Directory.GetFiles(Path.GetDirectoryName(destinationFile), Path.GetFileNameWithoutExtension(destinationFile) + "*");

                        // rename to prevent overwrite
                        destinationFile = Path.Combine(Path.GetDirectoryName(destinationFile), String.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(destinationFile), sameNamedFiles.Length.ToString("D4"), Path.GetExtension(destinationFile)));
                    }

                    File.Copy(path, destinationFile);
                    File.Delete(path);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
            }
        }