示例#1
0
        /// <summary>
        /// Generates the multi-page thumbnails 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 GenerateMultiThumbs(MSEEncoder.MediaItem mediaItem,
            string filename, string outputDirectory)
        {
            double thumbAspectRatio = TNSettings.ThumbAspectRatio;

            ThumbnailPageLayout container = new ThumbnailPageLayout (TNSettings);
            ThumbnailGrid tgrid;

            if (thumbAspectRatio < 1.44)
                tgrid =
                    ThumbnailGrid.CreateUsingNColumns (container, TNSettings,
                                                       thumbAspectRatio, true,
                                                       _debug);
            else
                tgrid =
                    ThumbnailGrid.CreateUsingNRows (container, TNSettings,
                                                    thumbAspectRatio, true,
                                                    _debug);

            container.AdjustSize (tgrid);

            //ThumbnailGrid tgrid = ThumbnailGrid.CreateUsingNRows (container, _maxMultiRows, _multiColumns,
            //                                                      aspectRatio, _mThreshold);
            //container.adjustWidth (tgrid.NColumns, tgrid.ThumbWidth);

            if (outputDirectory == null)
                outputDirectory = GetDirectoryName (filename);
            string fixedFilename = System.IO.Path.GetFileNameWithoutExtension (filename);
            fixedFilename = fixedFilename.Replace ("{", "(");
            fixedFilename = fixedFilename.Replace ("}", ")");
            string outTemplate = fixedFilename + "_page{0:D4}.jpg";

            int nThumbsPerPage = tgrid.NColumns * tgrid.NRows;
            int nThumbs = (int) ((TNSettings.End - TNSettings.Start).TotalSeconds /
                                  TNSettings.Interval.TotalSeconds) + 1;

            // start adjustment to make thumbnails occur at _interval seconds
            int nExtraStartSeconds = (int) (TNSettings.Start.TotalSeconds %
                                            TNSettings.Interval.TotalSeconds);
            int nStartIntervals = (int) (TNSettings.Start.TotalSeconds /
                                         TNSettings.Interval.TotalSeconds);
            if (nExtraStartSeconds != 0)
                {
                nThumbs++;
                nStartIntervals++;
                }

            int nExtraEndSeconds = (int) (TNSettings.End.TotalSeconds %
                                          TNSettings.Interval.TotalSeconds);
            int nEndIntervals = (int) (TNSettings.End.TotalSeconds /
                                       TNSettings.Interval.TotalSeconds);
            TimeSpan adjustedEnd = TNSettings.End;
            if (nExtraEndSeconds != 0)
                {
                nThumbs++;
                adjustedEnd = TNSettings.End;
                //adjustedEnd = new TimeSpan (0, 0, nEndIntervals *
                //                                 (int) tnSettings.Interval.TotalSeconds);
                }

            int nPages = (int) ((float) nThumbs / nThumbsPerPage + 0.5);
            if (nPages * nThumbsPerPage < nThumbs)
                nPages++;

            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")
                    );
                }

            THelper.Information ("Duration {0} - Generating {1} thumbs every {2} seconds on {3} pages.",
                                mediaItem.FileDuration.ToString (@"h\:mm\:ss"),
                                nThumbs, TNSettings.Interval.TotalSeconds, nPages);

            ThumbnailMultiWriter mwriter =
                new ThumbnailMultiWriter (this, tgrid,
                                          outputDirectory, filename, outTemplate,
                                          TNSettings.Interval,
                                          stats, mediaItem.FileDuration, nPages,
                                          TNSettings.ScaleFactor, _debug);

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

            if (nExtraStartSeconds != 0)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (mediaItem, currentTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    mwriter.Add (resized, currentTime, 0, TimeSpan.Zero);
                    thumbCount++;
                    }
                currentTime = new TimeSpan (0, 0, 0, 0,
                                            (int) (nStartIntervals *
                                                   TNSettings.Interval.TotalMilliseconds));
                }

            while (currentTime <= adjustedEnd)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (mediaItem, currentTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    mwriter.Add (resized, currentTime, 0, TimeSpan.Zero);
                    thumbCount++;
                    }
                currentTime += TNSettings.Interval;
                }

            if (nExtraEndSeconds != 0 && thumbCount < nThumbs)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (mediaItem, TNSettings.End,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    mwriter.Add (resized, TNSettings.End, 0, TimeSpan.Zero);
                    thumbCount++;
                    }
                }

            mwriter.Close ();
            return thumbCount;
        }
示例#2
0
        /// <summary>
        /// Generates the Detail thumbnail pages.
        /// </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 GenerateDetailThumbs(AVFileSet avFiles,
            string displayFilename,
            string outTemplate,
            string outputDirectory)
        {
            ThumbnailGrid tgrid = CreateThumbnailGrid (TNSettings.LayoutMode,
                                                       TNSettings.DetailThumbs,
                                                       TNSettings.DetailColumns,
                                                       TNSettings.DetailRows,
                                                       TNSettings.DetailThreshold);

            //ThumbnailGrid tgrid = ThumbnailGrid.CreateUsingNRows (layout, _maxDetailRows, _detailColumns,
            //                                                      aspectRatio, _mThreshold);
            //layout.adjustWidth (tgrid.NColumns, tgrid.ThumbWidth);

            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);

            int nThumbsPerPage = tgrid.NColumns * tgrid.NRows;
            int nThumbs = (int) ((TNSettings.End - TNSettings.Start).TotalSeconds /
                                  TNSettings.Interval.TotalSeconds) + 1;

            // start adjustment to make thumbnails occur at _interval seconds
            int nExtraStartSeconds = (int) (TNSettings.Start.TotalSeconds %
                                            TNSettings.Interval.TotalSeconds);
            int nStartIntervals = (int) (TNSettings.Start.TotalSeconds /
                                         TNSettings.Interval.TotalSeconds);
            if (nExtraStartSeconds != 0)
                {
                nThumbs++;
                nStartIntervals++;
                }

            int nExtraEndSeconds = (int) (TNSettings.End.TotalSeconds %
                                          TNSettings.Interval.TotalSeconds);
            int nEndIntervals = (int) (TNSettings.End.TotalSeconds /
                                       TNSettings.Interval.TotalSeconds);
            TimeSpan adjustedEnd = TNSettings.End;
            if (nExtraEndSeconds != 0)
                {
                nThumbs++;
                adjustedEnd = TNSettings.End;
                //adjustedEnd = new TimeSpan (0, 0, nEndIntervals *
                //                                 (int) tnSettings.Interval.TotalSeconds);
                }

            int nPages = (int) ((float) nThumbs / nThumbsPerPage + 0.5);
            if (nPages * nThumbsPerPage < nThumbs)
                nPages++;

            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")
                    );
                }

            THelper.Information (
                "Generating {0} {1}x{2} thumbnails every {3} seconds on {4} {5}x{6} Detail pages.",
                nThumbs,
                tgrid.ThumbWidth, tgrid.ThumbHeight,
                TNSettings.Interval.TotalSeconds, nPages,
                tgrid.NColumns, tgrid.NRows);

            ThumbnailMultiWriter mwriter =
                new ThumbnailMultiWriter (this, tgrid,
                                          outputDirectory, displayFilename, outTemplate,
                                          avFiles.Count > 1 ? avFiles.Count : 0,
                                          TNSettings.Interval,
                                          stats, avFiles.TotalDuration, nPages);

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

            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 (nExtraStartSeconds != 0 && thumbGenerator != null)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (thumbGenerator, currentTime - fileStartTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    if (avFiles.Count > 1)
                        mwriter.Add (resized, currentTime, highlight, avFileNum, fileStartTime);
                    else
                        mwriter.Add (resized, currentTime, highlight, 0, TimeSpan.Zero);
                    thumbCount++;
                    highlight = false;
                    }
                currentTime = new TimeSpan (0, 0, 0, 0,
                                            (int) (nStartIntervals *
                                                   TNSettings.Interval.TotalMilliseconds));
                }

            while (currentTime <= adjustedEnd && thumbGenerator != null)
                {
                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;
                            }
                        mwriter.Close ();
                        return thumbCount;
                        }
                    }

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

            if (nExtraEndSeconds != 0 && thumbCount < nThumbs &&
                thumbGenerator != null)
                {
                using (System.Drawing.Bitmap resized =
                    GenerateThumbnail (thumbGenerator, TNSettings.End - fileStartTime,
                                       tgrid.ThumbWidth, tgrid.ThumbHeight, TNSettings.SrcRect))
                    {
                    if (avFiles.Count > 1)
                        mwriter.Add (resized, TNSettings.End, highlight, avFileNum, fileStartTime);
                    else
                        mwriter.Add (resized, TNSettings.End, highlight, 0, TimeSpan.Zero);
                    thumbCount++;
                    highlight = false;
                    }
                }
            if (thumbGenerator != null)
                {
                thumbGenerator.Dispose ();
                thumbGenerator = null;
                }

            mwriter.Close ();
            return thumbCount;
        }