using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; namespace MCEiPodSync { public partial class Main { const string MCE_Ext = @".dvr-ms"; const string MPEG_Ext = @".mpg"; const string MP4_Ext = @".mp4"; const string MCE_Recorded_TV_Dir = @"C:\Documents and Settings\All Users\Documents\Recorded TV\"; const string MPEG_Conversion_Dir = @"C:\Documents and Settings\All Users\Documents\MPEG Conversion\"; const string MP4_Video_Dir = @"C:\Documents and Settings\username\My Documents\My Music\Recorded TV\"; const string DVRMStoMPEG_Path = @"C:\Program Files\DVRMSToolbox\DVRMStoMPEG.exe"; const string FFMPEG_Path = @"C:\Program Files\FFMPEG\ffmpeg.exe"; const string ITLU_EXE_Path = @"C:\Program Files\iTunes Library Updater\ITLUconsole.exe"; const string ITLU_Profile_Path = @"C:\Program Files\iTunes Library Updater\itlu.itlu"; void Run() { DirectoryInfo diMCE = new DirectoryInfo(MCE_Recorded_TV_Dir); DirectoryInfo diMPEG = new DirectoryInfo(MPEG_Conversion_Dir); DirectoryInfo diMP4 = new DirectoryInfo(MP4_Video_Dir); List lMCEFiles = new List(); List lMPEGFiles = new List(); List lMP4Files = new List(); // initialize the lists of files in the 3 directories AddAllFilesWithExtension(diMCE, lMCEFiles, MCE_Ext); AddAllFilesWithExtension(diMPEG, lMPEGFiles, MPEG_Ext); AddAllFilesWithExtension(diMP4, lMP4Files, MP4_Ext); // delete everything in the MPEG folder, there should be nothing there DeleteAllFiles(lMPEGFiles); // delete any MP4s for which there is no longer a corresponding MCE file DeleteFiles(lMP4Files, GetFullNames(MP4_Video_Dir, GetItemsOnlyInSecondList(GetShortNames(lMCEFiles), GetShortNames(lMP4Files)), MP4_Ext)); // go through all of the MCE files for which there is not yet an MP4 file foreach (string mce_file_name in GetItemsOnlyInSecondList(GetShortNames(lMP4Files), GetShortNames(lMCEFiles))) { FileInfo mce_file = GetFullName(MCE_Recorded_TV_Dir, mce_file_name, MCE_Ext); // convert the MCE file to an MPEG file FileInfo mpeg_file = ConvertMCEToMPEG(mce_file); // if something goes wrong, skip to the next file if (null == mpeg_file || !mpeg_file.Exists) { Status("Error converting file \"" + mce_file.Name + "\" to an MPEG."); continue; } else Status("Successfully created file \"" + mpeg_file.Name + "\""); lMPEGFiles.Add(mpeg_file); // convert the MPEG file to an MP4 file FileInfo mp4_file = ConvertMPEGToMP4(mpeg_file); // delete the MPEG file DeleteFile(lMPEGFiles, mpeg_file); if (null == mp4_file || !mp4_file.Exists) Status("Error converting file \"" + mpeg_file.Name + "\" to an MP4."); else Status("Successfully created file \"" + mp4_file.Name + "\""); lMP4Files.Add(mp4_file); } // sync the newly created videos to the iPod SyncVideosToIPod(); } FileInfo ConvertMCEToMPEG(FileInfo mce_file) { Status("Converting file to MPEG: \"" + mce_file.Name + "\""); string mpeg_file_name = MPEG_Conversion_Dir + GetShortName(mce_file) + MPEG_Ext; // convert it using DVRMStoMPEG Process p = Process.Start(DVRMStoMPEG_Path, " /if=\"" + mce_file.FullName + "\" /of=\"" + mpeg_file_name + "\" /act=dvrmstompg"); p.WaitForExit(); // check if the program returned an error code if (p.ExitCode != 0) return null; return new FileInfo(mpeg_file_name); } FileInfo ConvertMPEGToMP4(FileInfo mpeg_file) { Status("Converting file to MP4: \"" + mpeg_file.Name + "\""); string mp4_file_name = MP4_Video_Dir + GetShortName(mpeg_file) + MP4_Ext; // convert it using ffmpeg Process p = Process.Start(FFMPEG_Path, " -i \"" + mpeg_file.FullName + "\" -f mp4 -vcodec mpeg4 -s 320x240 -b 512 \"" + mp4_file_name + "\""); p.WaitForExit(); // check if the program returned an error code if (p.ExitCode != 0) return null; return new FileInfo(mp4_file_name); } void SyncVideosToIPod() { Status("Starting iTunes/iPod sync..."); Process p = Process.Start(ITLU_EXE_Path, " /p:\"" + ITLU_Profile_Path + "\""); p.WaitForExit(); if (p.ExitCode != 0) Status("Error calling ITLU"); else Status("Success!"); } void AddAllFilesWithExtension(DirectoryInfo directory, List files, string extension) { foreach (FileInfo file in directory.GetFiles()) { if (file.Name.EndsWith(extension)) files.Add(file); } } string GetShortName(FileInfo file) { return file.Name.Substring(0, file.Name.Length - file.Extension.Length); } List GetShortNames(List files) { List names = new List(); foreach (FileInfo file in files) names.Add(GetShortName(file)); return names; } FileInfo GetFullName(string directory_name, string short_name, string extension) { return new FileInfo(directory_name + short_name + extension); } List GetFullNames(string directory_name, List short_names, string extension) { List files = new List(); foreach (string file in short_names) files.Add(GetFullName(directory_name, file, extension)); return files; } List GetItemsOnlyInSecondList(List list1, List list2) { List items = new List(); foreach (string item in list2) { if (!list1.Contains(item)) items.Add(item); } return items; } void DeleteFile(List file_list, FileInfo file) { Status("Deleting file: " + file.Name); file_list.Remove(file); file.Delete(); } void DeleteFiles(List master_list, List files_to_delete) { foreach (FileInfo file in files_to_delete) DeleteFile(master_list, file); } void DeleteAllFiles(List files) { while (files.Count > 0) DeleteFile(files, files[0]); } } }