Exemple #1
0
        void ProcessMediaFile(ProcessFileInfo fi)
        {
            ReportProgress(Path.GetFileName(fi.Filepath));

            try
            {
                // First, check for duplicate
                if (DeDuplicate)
                {
                    var hash = CalculateMd5Hash(fi.Filepath);
                    if (!m_duplicateHash.Add(hash))
                    {
                        ReportProgress("   Removing Duplicate");
                        File.Delete(fi.Filepath);
                        ++m_duplicatesRemoved;
                        return;
                    }
                }

                using (var mdf = new MediaFile(fi.Filepath, Path.GetFileName(fi.OriginalFilepath)))
                {
                    mdf.OriginalDateCreated  = fi.OriginalDateCreated;
                    mdf.OriginalDateModified = fi.OriginalDateModified;

                    if (SetOrderedNames && mdf.SetOrderedName())
                    {
                        fi.Filepath = mdf.Filepath;
                        ReportProgress("   Rename to: " + Path.GetFileName(mdf.Filepath));
                    }

                    if (SaveOriginalFilaname)
                    {
                        if (mdf.SaveOriginalFilename())
                        {
                            ReportProgress("   Original filename saved.");
                        }
                    }

                    if (SetUuid)
                    {
                        if (mdf.SetUuid())
                        {
                            ReportProgress("   Set UUID.");
                        }
                    }

                    bool hasCreationDate = mdf.DeterimineCreationDate(AlwaysSetDate);
                    if (hasCreationDate)
                    {
                        ReportProgress($"   Date: {mdf.CreationDate} ({mdf.CreationDate.Date.Kind}) from {mdf.CreationDateSource}.");
                    }

                    bool hasTimezone = mdf.DetermineTimezone(AlwaysSetDate);
                    if (hasTimezone)
                    {
                        ReportProgress($"   Timezone: {Format(mdf.Timezone)} from {mdf.TimezoneSource}.");
                    }

                    if (Transcode && !mdf.IsPreferredFormat)
                    {
                        ReportProgress($"   Transcode to: {mdf.PreferredFormat} ({mdf.Duration.ToString(@"hh\:mm\:ss")})");
                        if (mdf.TranscodeToPreferredFormat(msg => ReportStatus(msg)))
                        {
                            fi.Filepath = mdf.Filepath;
                            ReportProgress($"      Transcoded to: {Path.GetFileName(mdf.Filepath)}");
                        }
                        else
                        {
                            ReportProgress("      Transcode failed; original format retained.");
                        }
                    }

                    if (SetWidth != 0 || SetHeight != 0)
                    {
                        ReportProgress($"   Scale {((mdf.Orientation != 1) ? "and Autorotate" : string.Empty)}");
                        mdf.ScaleAndRotateToVertical(SetWidth, SetHeight);
                    }
                    else if (AutoRotate && mdf.Orientation != 1)
                    {
                        ReportProgress("   Autorotate");
                        mdf.RotateToVertical();
                    }

                    if (SetDateTo != null)
                    {
                        mdf.SetDate(SetDateTo);
                        ReportProgress($"   Date set to: {SetDateTo}");
                    }

                    if (ShiftDateBy.HasValue && mdf.ShiftDate(ShiftDateBy.Value))
                    {
                        ReportProgress($"   Date shifted to: {mdf.CreationDate}");
                    }

                    if (SetTimezoneTo != null)
                    {
                        bool dstActive;
                        if (!hasCreationDate)
                        {
                            ReportProgress("   ERROR: Cannot set timezone; file does not have a creationDate set.");
                        }
                        else if (mdf.SetTimezone(SetTimezoneTo, out dstActive))
                        {
                            ReportProgress($"   Timezone set to: {mdf.Timezone} {(dstActive ? "(DST)" : "(Standard)")}");
                            // Change to forceLocal date to suppress timezone part when rendering
                            var localDate = mdf.CreationDate;
                            localDate = new FileMeta.DateTag(localDate.Date, FileMeta.TimeZoneTag.ForceLocal, localDate.Precision);
                            ReportProgress($"   Date: {localDate} (Local)");
                        }
                        else
                        {
                            ReportProgress($"   ERROR: Failed to set timezone.");
                        }
                    }

                    if (ChangeTimezoneTo != null)
                    {
                        bool dstActive;
                        if (!hasCreationDate)
                        {
                            ReportProgress("   ERROR: Cannot change timezone; file does not have a creationDate set.");
                        }
                        else if (mdf.Timezone == null || mdf.Timezone.Kind != FileMeta.TimeZoneKind.Normal)
                        {
                            ReportProgress("   ERROR: Cannot change timezone; file does not have an existing timezone. Use -setTimezone first.");
                        }
                        else if (mdf.ChangeTimezone(ChangeTimezoneTo, out dstActive))
                        {
                            ReportProgress($"   Timezone changed to: {mdf.Timezone} {(dstActive ? "(DST)" : "(Standard)")}");
                            // Change to forceLocal date to supporess timezone part when rendering
                            var localDate = mdf.CreationDate;
                            localDate = new FileMeta.DateTag(localDate.Date, FileMeta.TimeZoneTag.ForceLocal, localDate.Precision);
                            ReportProgress($"   Date: {localDate} (Local)");
                        }
                        else
                        {
                            ReportProgress($"   ERROR: Failed to change timezone.");
                        }
                    }

                    if (MetadataFromFilename != SetMode.DoNothing)
                    {
                        mdf.MetadataFromFilename(MetadataFromFilename == SetMode.SetAlways);
                    }

                    if (AddKeywords.Count > 0)
                    {
                        if (mdf.AddKeywords(AddKeywords))
                        {
                            ReportProgress($"   Tag(s) Added.");
                        }
                    }

                    if (mdf.CommitMetadata())
                    {
                        ReportProgress("   Metadata updated.");
                    }

                    if (SetMetadataNames && mdf.MetadataToFilename())
                    {
                        fi.Filepath = mdf.Filepath;
                        ReportProgress("   Rename to: " + Path.GetFileName(mdf.Filepath));
                    }

                    if (SortBy != DatePathType.None && !string.IsNullOrEmpty(DestinationDirectory))
                    {
                        if (hasCreationDate)
                        {
                            if (mdf.MoveFileToDatePath(DestinationDirectory, SortBy))
                            {
                                fi.Filepath = mdf.Filepath;
                                ReportProgress("   AutoSorted to: " + Path.GetDirectoryName(mdf.Filepath));
                            }
                            else
                            {
                                ReportProgress("   Existing filename conflicts with autosort path.");
                            }
                        }
                        else
                        {
                            ReportProgress("   No date determined - cannot autosort.");
                        }
                    }

                    if (UpdateFileSystemDateCreated && mdf.UpdateFileSystemDateCreated())
                    {
                        ReportProgress("   Updated FS Date Created.");
                    }

                    if (UpdateFileSystemDateModified && mdf.UpdateFileSystemDateModified())
                    {
                        ReportProgress("   Updated FS Date Modified.");
                    }
                }
            }
            catch (Exception err)
            {
                ReportProgress($"   Error: {err.Message.Trim()}");
            }
        }