Keep Modification Date (filestamp) from a MTS video to a MP4 Video - Powershell Script

3 min read 20-10-2024
Keep Modification Date (filestamp) from a MTS video to a MP4 Video - Powershell Script

When working with video files, particularly when converting them from one format to another, it's essential to maintain the original metadata, including the modification date. This guide focuses on using PowerShell to convert MTS video files to MP4 format while keeping the original modification date intact.

Problem Scenario

You may have encountered the challenge of converting MTS video files to MP4 format while losing the original file's modification date in the process. Below is an example of a PowerShell script that demonstrates this process, along with the problem it addresses.

Original PowerShell Code Example:

# Example PowerShell code to convert MTS to MP4 (modification date not preserved)
$inputFile = "C:\Path\To\Your\Video.mts"
$outputFile = "C:\Path\To\Your\Video.mp4"

# Command to convert MTS to MP4 (using FFmpeg)
Start-Process -FilePath "C:\Path\To\ffmpeg.exe" -ArgumentList "-i `"$inputFile`" -c:v copy -c:a copy `"$outputFile`""

While the above script effectively converts the video file, it does not preserve the modification date, leading to a potential loss of essential file information.

A Solution to Preserve Modification Dates

To retain the modification date of the original MTS file during the conversion process, we can enhance our PowerShell script. Below is the revised version of the PowerShell script that achieves this:

Revised PowerShell Code:

$inputFile = "C:\Path\To\Your\Video.mts"
$outputFile = "C:\Path\To\Your\Video.mp4"

# Command to convert MTS to MP4 (using FFmpeg)
Start-Process -FilePath "C:\Path\To\ffmpeg.exe" -ArgumentList "-i `"$inputFile`" -c:v copy -c:a copy `"$outputFile`""

# Retrieve and set the modification date
$modificationDate = (Get-Item $inputFile).LastWriteTime
(Get-Item $outputFile).LastWriteTime = $modificationDate

Explanation of the Code

  1. Input and Output Paths: The script defines the input path for the MTS file and the desired output path for the MP4 file.
  2. FFmpeg Command: It utilizes FFmpeg to convert the MTS video to MP4 format without re-encoding the audio and video streams (using -c:v copy and -c:a copy).
  3. Modification Date Preservation: After conversion, it retrieves the original file's last modification date using Get-Item and assigns it to the new MP4 file.

Practical Examples

Let's say you have a folder filled with MTS video files from a family vacation. You would like to convert these to MP4 for easier sharing and storage while retaining the date they were filmed. By employing the above script, you can efficiently convert all files in a batch process while ensuring the integrity of the original metadata.

Additional Tips

  • Batch Processing: To process multiple files, you can easily modify the script to loop through all MTS files in a directory.

    $inputFolder = "C:\Path\To\Your\MTSFiles"
    $outputFolder = "C:\Path\To\Your\MP4Files"
    
    Get-ChildItem "$inputFolder\*.mts" | ForEach-Object {
        $inputFile = $_.FullName
        $outputFile = Join-Path $outputFolder "$($_.BaseName).mp4"
        
        Start-Process -FilePath "C:\Path\To\ffmpeg.exe" -ArgumentList "-i `"$inputFile`" -c:v copy -c:a copy `"$outputFile`""
        $modificationDate = (Get-Item $inputFile).LastWriteTime
        (Get-Item $outputFile).LastWriteTime = $modificationDate
    }
    
  • Dependencies: Ensure that FFmpeg is installed on your system, and the path to ffmpeg.exe in the script is correct.

Conclusion

Maintaining the modification date of MTS files when converting to MP4 can be achieved effortlessly with PowerShell and FFmpeg. By following the enhanced script provided, users can ensure that all vital metadata is preserved during the conversion process.

Useful Resources

By following this guide, you can streamline your video conversion tasks and keep your file organization in check. Happy converting!