예제 #1
0
        /// <summary>
        /// Generates the overview thumbnail page.
        /// </summary>
        /// <param name="avFiles">The <see cref="AVFileSet"/> to generate 
        /// thumbnails for.</param>
        /// <param name="displayFilename">The display name of the <see cref="AVFileSet"/>.</param>
        /// <param name="outTemplate">The template (string format) to use when generating
        /// output filenames.</param>
        /// <param name="outputDirectory">The fullpath of the output directory.</param>
        /// <returns>The number of thumbnails created.</returns>
        public int GenerateOverviewThumbs(AVFileSet avFiles,
            string displayFilename,
            string outTemplate,
            string outputDirectory)
        {
            ThumbnailGrid tgrid = CreateThumbnailGrid (TNSettings.LayoutMode,
                                                       TNSettings.OverviewThumbs,
                                                       TNSettings.OverviewColumns,
                                                       TNSettings.OverviewRows,
                                                       TNSettings.OverviewThreshold);

            System.Collections.Generic.IEnumerator<MSEEncoder.AudioVideoFile> avFilesEnumerator =
                avFiles.GetEnumerator();
            avFilesEnumerator.MoveNext();
            MSEEncoder.AudioVideoFile avFile = avFilesEnumerator.Current;
            int avFileNum = 1;
            bool highlight = avFiles.Count > 1 ? true : false;

            System.Drawing.Size videoSize = AVFileSet.GetVideoSize (avFile);

            #if true
            string stats;
            if (videoSize.Width != TNSettings.SrcRect.Width ||
                videoSize.Height != TNSettings.SrcRect.Height ||
                Math.Abs ((double) videoSize.Width / videoSize.Height -
                          TNSettings.ThumbAspectRatio) > 0.01)
                {
                stats = String.Format ("{0}x{1} ({2:F2}:1) [{3}x{4} ({5:F2}:1)] {6}  {7}",
                    TNSettings.SrcRect.Width,
                    TNSettings.SrcRect.Height,
                    TNSettings.ThumbAspectRatio,

                    videoSize.Width,
                    videoSize.Height,
                    (double) videoSize.Width /
                        videoSize.Height,

                    //getAudioStreamStats(mediaItem),
                    GetFileSizeString (avFiles.TotalFileSize),
                    System.IO.File.GetLastWriteTime (avFile.FileName).ToString ("g")
                    );
                }
            else
                {
                stats = String.Format ("{0}x{1} ({2:F2}:1) {3}  {4}",
                    videoSize.Width,
                    videoSize.Height,
                    TNSettings.ThumbAspectRatio,
                    //getAudioStreamStats(mediaItem),
                    GetFileSizeString (avFiles.TotalFileSize),
                    System.IO.File.GetLastWriteTime (avFile.FileName).ToString ("g")
                    );
                }

            #else
            string stats = String.Format ("{0}x{1} ({2:F2}:1) [{3}x{4} {5}x{6}]  {7}  {8}",
                videoSize.Width,
                videoSize.Height,
                (double) videoSize.Width /
                    videoSize.Height,
                //getAudioStreamStats(mediaItem),
                tgrid.ThumbWidth, tgrid.ThumbHeight,
                tgrid.NColumns, tgrid.NRows,
                GetFileSizeString (mediaItem.MainMediaFile.FileSize),
                System.IO.File.GetLastWriteTime (displayFilename).ToString ("g")
                );
            #endif

            THelper.Information ("Generating {0} {1}x{2} thumbnails on a {3}x{4} Overview page.",
                                 tgrid.NThumbs,
                                 tgrid.ThumbWidth, tgrid.ThumbHeight,
                                 tgrid.NColumns, tgrid.NRows);

            ThumbnailWriter writer =
                new ThumbnailWriter (this, tgrid, outputDirectory, displayFilename, outTemplate,
                                     avFiles.Count > 1 ? avFiles.Count : 0,
                                     stats, avFiles.TotalDuration);

            double intervalSeconds =
                ((TNSettings.End.TotalSeconds - TNSettings.Start.TotalSeconds) /
                 (tgrid.NColumns * tgrid.NRows - 1));
            int milliseconds =
                (int) ((intervalSeconds - Math.Truncate (intervalSeconds)) * 1000 + 0.5);
            TimeSpan interval = new TimeSpan (0, 0, 0, (int) intervalSeconds, milliseconds);

            int nThumbsAdded = 0;
            TimeSpan currentTime = TNSettings.Start;
            TimeSpan fileStartTime = new TimeSpan(0,0,0);
            MSEEncoder.ThumbnailGenerator thumbGenerator = avFile.CreateThumbnailGenerator (videoSize);

            while (currentTime <= TNSettings.End)
                {
                while (currentTime > fileStartTime + AVFileSet.GetVideoDuration(avFile))
                    {
                    if (thumbGenerator != null)
                        {
                        thumbGenerator.Dispose();
                        thumbGenerator = null;
                        }

                    if (!avFilesEnumerator.MoveNext ())
                        {
                        avFile = null;
                        break;
                        }

                    fileStartTime += AVFileSet.GetVideoDuration(avFile);
                    avFile = avFilesEnumerator.Current;
                    avFileNum++;
                    highlight = true;
                    thumbGenerator = avFile.CreateThumbnailGenerator (videoSize);
                    }
                if (thumbGenerator == null)
                    break;
                if (BGWorker != null)
                    {
                    if (BGWorker.CancellationPending)
                        {
                        if (thumbGenerator != null)
                            {
                            thumbGenerator.Dispose ();
                            thumbGenerator = null;
                            }
                        writer.Close ();
                        return nThumbsAdded;
                        }
                    }

                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (thumbGenerator, currentTime - fileStartTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    if (avFiles.Count > 1)
                        writer.Add (resized, currentTime, highlight, avFileNum);
                    else
                        writer.Add (resized, currentTime, highlight, 0);
                    highlight = false;
                    nThumbsAdded++;
                    }
                currentTime += interval;
                }

            // Last thumb should always be end time.
            if (nThumbsAdded < tgrid.NThumbs &&
                thumbGenerator != null)
                {
                using (System.Drawing.Bitmap resized =
                       GenerateThumbnail (thumbGenerator, TNSettings.End - fileStartTime,
                                          tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    if (avFiles.Count > 1)
                        writer.Add (resized, TNSettings.End, highlight, avFileNum);
                    else
                        writer.Add (resized, TNSettings.End, highlight, 0);
                    nThumbsAdded++;
                    }
                }
            if (thumbGenerator != null)
                {
                thumbGenerator.Dispose ();
                thumbGenerator = null;
                }

            writer.Close ();

            return tgrid.NThumbs;
        }
예제 #2
0
        /// <summary>
        /// Generates the overview thumbnail page OBSOLETE.
        /// </summary>
        /// <param name="mediaItem">The 
        /// <see cref="Microsoft.Expression.Encoder.MediaItem"/> to generate 
        /// thumbnails for.</param>
        /// <param name="filename">The display name of the file to generate thumbs for.</param>
        /// <param name="outputDirectory">The output directory.</param>
        /// <returns>The number of thumbnails created.</returns>
        public int GenerateOverviewThumbs(MSEEncoder.MediaItem mediaItem,
            string filename, string outputDirectory)
        {
            double thumbAspectRatio = TNSettings.ThumbAspectRatio;

            ThumbnailPageLayout container =
                new ThumbnailPageLayout (TNSettings);

            ThumbnailGrid tgrid;
            if (thumbAspectRatio < 1.36)
                tgrid =
                    ThumbnailGrid.CreateUsingNColumns (container, TNSettings,
                                                       thumbAspectRatio, false,
                                                       _debug);
            else
                tgrid =
                    ThumbnailGrid.CreateUsingNRows (container, TNSettings,
                                                    thumbAspectRatio, false,
                                                    _debug);
            container.AdjustSize (tgrid);

            if (outputDirectory == null)
                outputDirectory = GetDirectoryName (filename);
            string outTemplate =  System.IO.Path.GetFileNameWithoutExtension (filename) +
                                  "_overview.jpg";

            #if true
            string stats;
            if (mediaItem.OriginalVideoSize.Width != TNSettings.SrcRect.Width ||
                mediaItem.OriginalVideoSize.Height != TNSettings.SrcRect.Height ||
                Math.Abs ((double) mediaItem.OriginalVideoSize.Width /
                    mediaItem.OriginalVideoSize.Height -
                    TNSettings.ThumbAspectRatio) > 0.01)
                {
                stats = String.Format ("{0}x{1} ({2:F2}:1) [{3}x{4} ({5:F2}:1)] {6}  {7}",
                    TNSettings.SrcRect.Width,
                    TNSettings.SrcRect.Height,
                    TNSettings.ThumbAspectRatio,

                    mediaItem.OriginalVideoSize.Width,
                    mediaItem.OriginalVideoSize.Height,
                    (double) mediaItem.OriginalVideoSize.Width /
                        mediaItem.OriginalVideoSize.Height,

                        //getAudioStreamStats(mediaItem),
                    GetFileSizeString (mediaItem.MainMediaFile.FileSize),
                    System.IO.File.GetLastWriteTime (filename).ToString ("g")
                    );
                }
            else
                {
                stats = String.Format ("{0}x{1} ({2:F2}:1) {3}  {4}",
                    mediaItem.OriginalVideoSize.Width,
                    mediaItem.OriginalVideoSize.Height,
                    TNSettings.ThumbAspectRatio,
                    //getAudioStreamStats(mediaItem),
                    GetFileSizeString (mediaItem.MainMediaFile.FileSize),
                    System.IO.File.GetLastWriteTime (filename).ToString ("g")
                    );
                }

            #else
            string stats = String.Format ("{0}x{1} ({2:F2}:1) [{3}x{4} {5}x{6}]  {7}  {8}",
                mediaItem.OriginalVideoSize.Width,
                mediaItem.OriginalVideoSize.Height,
                (double) mediaItem.OriginalVideoSize.Width /
                    mediaItem.OriginalVideoSize.Height,
                //getAudioStreamStats(mediaItem),
                tgrid.ThumbWidth, tgrid.ThumbHeight,
                tgrid.NColumns, tgrid.NRows,
                GetFileSizeString (mediaItem.MainMediaFile.FileSize),
                System.IO.File.GetLastWriteTime (filename).ToString ("g")
                );
            #endif

            THelper.Information ("Generating {0} overview page thumbs.",
                                 tgrid.NThumbs);

            ThumbnailWriter writer =
                new ThumbnailWriter (this, tgrid, outputDirectory, filename, outTemplate,
                                     stats, mediaItem.FileDuration,
                                     TNSettings.ScaleFactor, _debug);

            double intervalSeconds =
                ((TNSettings.End.TotalSeconds - TNSettings.Start.TotalSeconds) /
                 (tgrid.NColumns * tgrid.NRows - 1));
            int milliseconds =
                (int) ((intervalSeconds - Math.Truncate (intervalSeconds)) * 1000 + 0.5);
            TimeSpan interval = new TimeSpan (0, 0, 0, (int) intervalSeconds, milliseconds);

            int nThumbsAdded = 0;
            TimeSpan currentTime = TNSettings.Start;

            while (currentTime <= TNSettings.End)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (mediaItem, currentTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    writer.Add (resized, currentTime);
                    nThumbsAdded++;
                    }
                currentTime += interval;
                }

            // Last thumb should always be end time.
            if (nThumbsAdded < tgrid.NThumbs)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (mediaItem, TNSettings.End,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    writer.Add (resized, TNSettings.End);
                    nThumbsAdded++;
                    }
                }

            writer.Close ();

            return tgrid.NThumbs;
        }