Task 434: .MTS File Format

Task 434: .MTS File Format

File Format Specifications for .MTS

The .MTS file format is a container for AVCHD (Advanced Video Coding High Definition) video, developed by Sony and Panasonic. It is a subset of the Blu-ray Disc specification and uses the MPEG-2 Transport Stream (M2TS) container. The format supports H.264/MPEG-4 AVC video compression, Dolby Digital (AC-3) or Linear PCM audio, and optional subtitles. Files consist of 192-byte packets (188-byte MPEG-2 TS packet plus 4-byte arrival timestamp header). It supports resolutions up to 3840x2160, frame rates like 60p/50p/24p, and multichannel audio up to 7.1. The structure includes multiplexed streams in the STREAM directory of an AVCHD folder hierarchy, with associated metadata in .CLPI files.

  1. List of all the properties of this file format intrinsic to its file system:
  • File size (in bytes)
  • Creation time
  • Modification time
  • Access time
  • Permissions (mode)
  • Owner UID
  • Owner GID
  • Inode number
  • Device ID
  • Number of hard links
  1. Two direct download links for files of format .MTS:
  1. Ghost blog embedded HTML JavaScript for drag and drop .MTS file to dump properties:
Drag and Drop .MTS File Analyzer
Drag and drop a .MTS file here
  1. Python class for .MTS file:
import os
import time
import stat

class MTSFileHandler:
    def __init__(self, filepath):
        self.filepath = filepath
        if not self.filepath.lower().endswith('.mts'):
            raise ValueError("File must be .MTS format")
        self.stat = os.stat(self.filepath)

    def read_properties(self):
        props = {
            'File size (bytes)': self.stat.st_size,
            'Creation time': time.ctime(self.stat.st_ctime),
            'Modification time': time.ctime(self.stat.st_mtime),
            'Access time': time.ctime(self.stat.st_atime),
            'Permissions (mode)': oct(self.stat.st_mode),
            'Owner UID': self.stat.st_uid,
            'Owner GID': self.stat.st_gid,
            'Inode number': self.stat.st_ino,
            'Device ID': self.stat.st_dev,
            'Number of hard links': self.stat.st_nlink
        }
        return props

    def print_properties(self):
        props = self.read_properties()
        for key, value in props.items():
            print(f"{key}: {value}")

    def write_modification_time(self, new_mtime):
        os.utime(self.filepath, (self.stat.st_atime, new_mtime))

    def write_permissions(self, new_mode):
        os.chmod(self.filepath, new_mode)

# Example usage:
# handler = MTSFileHandler('example.mts')
# handler.print_properties()
# handler.write_modification_time(time.time())  # Set to current time
# handler.write_permissions(stat.S_IRUSR | stat.S_IWUSR)  # Set read/write for owner
  1. Java class for .MTS file:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Set;

public class MTSFileHandler {
    private final Path path;
    private final BasicFileAttributes attrs;

    public MTSFileHandler(String filepath) throws IOException {
        this.path = Paths.get(filepath);
        if (!filepath.toLowerCase().endsWith(".mts")) {
            throw new IllegalArgumentException("File must be .MTS format");
        }
        this.attrs = Files.readAttributes(path, BasicFileAttributes.class);
    }

    public void printProperties() throws IOException {
        PosixFileAttributes posixAttrs = Files.readAttributes(path, PosixFileAttributes.class);
        System.out.println("File size (bytes): " + attrs.size());
        System.out.println("Creation time: " + attrs.creationTime());
        System.out.println("Modification time: " + attrs.lastModifiedTime());
        System.out.println("Access time: " + attrs.lastAccessTime());
        System.out.println("Permissions (mode): " + posixAttrs.permissions());
        System.out.println("Owner UID equivalent (owner): " + posixAttrs.owner());
        System.out.println("Owner GID equivalent (group): " + posixAttrs.group());
        System.out.println("Inode number: " + attrs.fileKey());  // Approximation
        System.out.println("Device ID: N/A in Java standard");  // Not directly available
        System.out.println("Number of hard links: " + posixAttrs.isOther() ? "N/A" : "1+");  // Approximation
    }

    public void writeModificationTime(long newMtimeMillis) throws IOException {
        FileTime newMtime = FileTime.fromMillis(newMtimeMillis);
        Files.setLastModifiedTime(path, newMtime);
    }

    public void writePermissions(Set<PosixFilePermission> newPermissions) throws IOException {
        Files.setPosixFilePermissions(path, newPermissions);
    }

    // Example usage:
    // public static void main(String[] args) throws IOException {
    //     MTSFileHandler handler = new MTSFileHandler("example.mts");
    //     handler.printProperties();
    //     handler.writeModificationTime(System.currentTimeMillis());
    //     Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");
    //     handler.writePermissions(perms);
    // }
}
  1. JavaScript class for .MTS file (assuming Node.js for full FS access):
const fs = require('fs');

class MTSFileHandler {
    constructor(filepath) {
        if (!filepath.toLowerCase().endsWith('.mts')) {
            throw new Error('File must be .MTS format');
        }
        this.filepath = filepath;
        this.stat = fs.statSync(this.filepath);
    }

    readProperties() {
        return {
            'File size (bytes)': this.stat.size,
            'Creation time': this.stat.birthtime.toString(),
            'Modification time': this.stat.mtime.toString(),
            'Access time': this.stat.atime.toString(),
            'Permissions (mode)': this.stat.mode.toString(8),
            'Owner UID': this.stat.uid,
            'Owner GID': this.stat.gid,
            'Inode number': this.stat.ino,
            'Device ID': this.stat.dev,
            'Number of hard links': this.stat.nlink
        };
    }

    printProperties() {
        const props = this.readProperties();
        for (const [key, value] of Object.entries(props)) {
            console.log(`${key}: ${value}`);
        }
    }

    writeModificationTime(newMtimeSeconds) {
        fs.utimesSync(this.filepath, this.stat.atime, newMtimeSeconds);
    }

    writePermissions(newMode) {
        fs.chmodSync(this.filepath, newMode);
    }
}

// Example usage:
// const handler = new MTSFileHandler('example.mts');
// handler.printProperties();
// handler.writeModificationTime(Date.now() / 1000);
// handler.writePermissions(0o600);  // Read/write for owner
  1. C class for .MTS file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>
#include <unistd.h>

struct MTSFileHandler {
    char *filepath;
    struct stat statbuf;
};

struct MTSFileHandler* mts_create(const char *filepath) {
    if (strstr(filepath, ".mts") == NULL && strstr(filepath, ".MTS") == NULL) {
        fprintf(stderr, "File must be .MTS format\n");
        return NULL;
    }
    struct MTSFileHandler *handler = malloc(sizeof(struct MTSFileHandler));
    handler->filepath = strdup(filepath);
    if (stat(filepath, &handler->statbuf) != 0) {
        free(handler);
        return NULL;
    }
    return handler;
}

void mts_print_properties(struct MTSFileHandler *handler) {
    if (handler == NULL) return;
    printf("File size (bytes): %ld\n", handler->statbuf.st_size);
    printf("Creation time: %s", ctime(&handler->statbuf.st_ctime));
    printf("Modification time: %s", ctime(&handler->statbuf.st_mtime));
    printf("Access time: %s", ctime(&handler->statbuf.st_atime));
    printf("Permissions (mode): %o\n", handler->statbuf.st_mode);
    printf("Owner UID: %d\n", handler->statbuf.st_uid);
    printf("Owner GID: %d\n", handler->statbuf.st_gid);
    printf("Inode number: %ld\n", handler->statbuf.st_ino);
    printf("Device ID: %ld\n", handler->statbuf.st_dev);
    printf("Number of hard links: %ld\n", handler->statbuf.st_nlink);
}

int mts_write_modification_time(struct MTSFileHandler *handler, time_t new_mtime) {
    if (handler == NULL) return -1;
    struct utimbuf ut;
    ut.actime = handler->statbuf.st_atime;
    ut.modtime = new_mtime;
    return utime(handler->filepath, &ut);
}

int mts_write_permissions(struct MTSFileHandler *handler, mode_t new_mode) {
    if (handler == NULL) return -1;
    return chmod(handler->filepath, new_mode);
}

void mts_destroy(struct MTSFileHandler *handler) {
    if (handler) {
        free(handler->filepath);
        free(handler);
    }
}

// Example usage:
// int main() {
//     struct MTSFileHandler *handler = mts_create("example.mts");
//     if (handler) {
//         mts_print_properties(handler);
//         mts_write_modification_time(handler, time(NULL));
//         mts_write_permissions(handler, S_IRUSR | S_IWUSR);
//         mts_destroy(handler);
//     }
//     return 0;
// }