Source

makeiso / mkiso / mkiso.py

Full commit
#!/usr/bin/python
"""
Utility script to automate the process of converting multiple video files
in different formats into a DVD ISO with chapters specified by the -c or --chapters
switch (defaults to 10 minutes).

TODO: What other command line args are useful?  Maybe a switch to remove
      converted mpegs
"""
import argparse
import os
import re
import shutil
import subprocess

from utils import get_chapters, parse_args, run_cmd
from videotypes import AviFile, InvalidVideoFormatError, Mp4File, MkvFile


VALID_EXTENSIONS = ('avi', 'mkv', 'mpg', 'mp4')


class ISOCreator(object):
    """
    Class for converting videos into DVD ISOs
    """

    def __init__(self, video_files, output_file, verbose, chapters):
        self.video_files = video_files
        self.output_file = output_file
        self.converted_videos = []
        self.verbose = verbose
        self.chapters = chapters
        os.putenv("VIDEO_FORMAT", "NTSC")

    def convert_video(self, video):
        """
        Converts videos into DVD MPEG2 formats
        """
        if self.verbose:
            print "Converting %s to MPEG2..." % (video, )
        ext = video.split('.')[-1]
        mpg_file = ".".join(video.split('.')[:-1]) + ".mpg"
        if ext not in VALID_EXTENSIONS:
            raise InvalidVideoFormatError(video)
        elif ext == 'avi':
            avi = AviFile(video)
            success = avi.convert()
            if success:
                self.converted_videos.append(mpg_file)
        elif ext == 'mkv':
            mkv = MkvFile(video)
            success = mkv.convert()
            if success:
                self.converted_videos.append(mpg_file)
        elif ext == 'mpg':
            self.converted_videos.append(video)
        elif ext == 'mp4':
            mp4 = Mp4File(video)
            success = mp4.convert()
            if success:
                self.converted_videos.append(mpg_file)

    def dvdauthor(self):
        """
        Uses dvdauthor to create titles and chapters
        """
        if self.verbose:
            print "Running DVDAuthor to create titles and chapters..."
        xml = "<dvdauthor><vmgm /><titleset><titles><pgc>"
        for vid in self.converted_videos:
            xml += "<vob file=\"%s\" chapters=\"%s\" />" % (vid,
                get_chapters(self.chapters))
        xml += "</pgc></titles></titleset></dvdauthor>"
        fp = open("/tmp/dvd.xml", "w")
        fp.write(xml)
        fp.close()
        os.mkdir("/tmp/DVD")
        cmd = "dvdauthor -o /tmp/DVD -x /tmp/dvd.xml"
        run_cmd(cmd)
        cmd2 = "dvdauthor -T -o /tmp/DVD"
        run_cmd(cmd2)

    def mkiso(self):
        """
        Creates an ISO
        """
        if self.verbose:
            print "Creating the ISO..."
        cmd = "mkisofs -dvd-video -o %s /tmp/DVD" % (self.output_file, )
        run_cmd(cmd)

    def run(self):
        """
        Runs the main loop
        """
        for video in self.video_files:
            video = os.path.abspath(video)
            self.convert_video(video)
        self.dvdauthor()
        self.mkiso()
        self.cleanup()

    def cleanup(self):
        """
        Removed mpg files and any other non-necessary files created
        """
        if self.verbose:
            print "Cleaning up files..."
        shutil.rmtree("/tmp/DVD")
        os.remove("/tmp/dvd.xml")


def main():
    args = parse_args()
    creator = ISOCreator(args.video_files, args.output, args.verbose, args.chapters)
    creator.run()


if __name__ == "__main__":
    main()