예제 #1
0
파일: Program.cs 프로젝트: areynold/GoMoPho
        public static bool Read(string file, List <byte[]> byteSearch)
        {
            //var r1 = new ExifLib.ExifReader(file);
            //var m1 = MetadataExtractor.ImageMetadataReader.ReadMetadata(file);
            Console.Write("Searching " + file);
            var fileBytes  = System.IO.File.ReadAllBytes(file);
            int indexOfMp4 = -1;

            foreach (var search in byteSearch)
            {
                indexOfMp4 = new BoyerMoore(search).Search(fileBytes);
                if (indexOfMp4 >= 0)
                {
                    break;
                }
            }
            if (indexOfMp4 >= 0)
            {
                Console.WriteLine("   Found the video");
                WriteVideo(file, fileBytes, indexOfMp4);
                return(true);
            }
            else
            {
                Console.Write("   Did not find known video, trying generic approach");
                // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files says that a JPEG start of image with 0xFFD8 and ends with 0xFFD9.
                var endOfJpeg        = MovingPhotoExtraction.ToBytes("FF D9".Split(' '));
                var indexOfMp4_Part1 = MovingPhotoExtraction.ToBytes("00 00 00".Split(' '));
                var indexOfMp4_Part2 = MovingPhotoExtraction.ToBytes("66 74 79 70".Split(' '));
                var indexOfMp4_Part3 = MovingPhotoExtraction.ToBytes("6D 70 34 32".Split(' '));

                var bm = new BoyerMoore(endOfJpeg);

                var endOf = bm.Search(fileBytes);
                bm.SetPattern(indexOfMp4_Part2);
                var part2s = bm.SearchAll(fileBytes);
                bm.SetPattern(indexOfMp4_Part3);
                foreach (var part2 in part2s)
                {
                    if (part2 < endOf)
                    {
                        Console.Write("Not yet at end of jpeg");
                    }
                    var part3 = bm.SearchAll(fileBytes).FirstOrDefault(a => a > part2);
                    // part 3 is just a bit further than part2
                    if (part3 > part2 && part2 + 20 > part3)
                    {
                        var minus4 = fileBytes[part2 - 4];
                        var minus3 = fileBytes[part2 - 3];
                        var minus2 = fileBytes[part2 - 2];
                        var minus1 = fileBytes[part2 - 1];
                        if (minus4 == 0 && minus3 == 0 && minus2 == 0)
                        {
                            Console.WriteLine("... Found video via pattern search");
                            WriteVideo(file, fileBytes, part2 - 4);
                            return(true);
                        }
                        Console.WriteLine($"... Found part2 and 3, not 1: {minus4} {minus3} {minus2} {minus1}");
                    }
                }
                Console.WriteLine($"Failed to find the video out of {part2s.Count} possibilites");
                Console.WriteLine("Please report this output to https://github.com/cliveontoast/GoMoPho/issues and attach your moving image jpeg file");
            }
            return(false);
        }