[longomatch] Detect AVCHD files and ask to remux them to MP4



commit faae354008eea90424f4de6ce56ace26a239ab68
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sat Jul 30 14:27:01 2011 +0200

    Detect AVCHD files and ask to remux  them to MP4

 CesarPlayer/CesarPlayer.mdp                      |   21 ++--
 CesarPlayer/Makefile.am                          |    1 +
 CesarPlayer/Utils/MpegRemuxer.cs                 |  137 ++++++++++++++++++++++
 LongoMatch/Gui/Component/ProjectDetailsWidget.cs |    9 ++
 po/POTFILES.in                                   |    1 +
 5 files changed, 159 insertions(+), 10 deletions(-)
---
diff --git a/CesarPlayer/CesarPlayer.mdp b/CesarPlayer/CesarPlayer.mdp
index 9f16991..9d31d71 100644
--- a/CesarPlayer/CesarPlayer.mdp
+++ b/CesarPlayer/CesarPlayer.mdp
@@ -1,4 +1,14 @@
 <Project name="CesarPlayer" fileversion="2.0" DefaultNamespace="longomatch" language="C#" clr-version="Net_2_0" targetFramework="2.0" ctype="DotNetProject">
+  <Deployment.LinuxDeployData generatePcFile="False" />
+  <MonoDevelop.Autotools.MakefileInfo RelativeMakefileName="Makefile.am" RelativeConfigureInPath="../">
+    <BuildFilesVar Name="FILES" />
+    <DeployFilesVar />
+    <ResourcesVar Name="RESOURCES" />
+    <OthersVar />
+    <GacRefVar Name="REFERENCES" />
+    <AsmRefVar Name="REFERENCES" />
+    <ProjectRefVar Name="REFERENCES" />
+  </MonoDevelop.Autotools.MakefileInfo>
   <Configurations active="Release">
     <Configuration name="Debug" ctype="DotNetProjectConfiguration">
       <Output directory="." assembly="a" assemblyKeyFile="." />
@@ -56,6 +66,7 @@
     <File subtype="Code" buildaction="Compile" name="Capturer/CaptureProperties.cs" />
     <File subtype="Code" buildaction="Compile" name="Utils/Device.cs" />
     <File subtype="Code" buildaction="Compile" name="Common/Constants.cs" />
+    <File subtype="Code" buildaction="Compile" name="Utils/MpegRemuxer.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
@@ -66,14 +77,4 @@
     <ProjectReference type="Gac" localcopy="True" refto="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
   </References>
   <LanguageParameters ApplicationIcon="." CodePage="65001" ctype="CSharpProjectParameters" />
-  <Deployment.LinuxDeployData generatePcFile="False" />
-  <MonoDevelop.Autotools.MakefileInfo RelativeMakefileName="Makefile.am" RelativeConfigureInPath="../">
-    <BuildFilesVar Name="FILES" />
-    <DeployFilesVar />
-    <ResourcesVar Name="RESOURCES" />
-    <OthersVar />
-    <GacRefVar Name="REFERENCES" />
-    <AsmRefVar Name="REFERENCES" />
-    <ProjectRefVar Name="REFERENCES" />
-  </MonoDevelop.Autotools.MakefileInfo>
 </Project>
\ No newline at end of file
diff --git a/CesarPlayer/Makefile.am b/CesarPlayer/Makefile.am
index 843f85f..c1231dc 100644
--- a/CesarPlayer/Makefile.am
+++ b/CesarPlayer/Makefile.am
@@ -22,6 +22,7 @@ SOURCES = \
 	Utils/IFramesCapturer.cs \
 	Utils/FramesCapturer.cs \
 	Utils/IMetadataReader.cs \
+	Utils/MpegRemuxer.cs \
 	Utils/TimeString.cs \
 	Capturer/CaptureProperties.cs \
 	Capturer/GstCameraCapturer.cs \
diff --git a/CesarPlayer/Utils/MpegRemuxer.cs b/CesarPlayer/Utils/MpegRemuxer.cs
new file mode 100644
index 0000000..4d9f48f
--- /dev/null
+++ b/CesarPlayer/Utils/MpegRemuxer.cs
@@ -0,0 +1,137 @@
+// 
+//  Copyright (C) 2011 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Threading;
+using Mono.Unix;
+using GLib;
+using Gtk;
+
+namespace LongoMatch.Video.Utils
+{
+	public class MpegRemuxer
+	{
+		private static string[] EXTENSIONS =  {"mts", "m2ts", "m2t", "ts", "mpeg", "mpg"};
+		private string filepath;
+		private string newFilepath;
+		private Dialog dialog;
+		private ProgressBar pb;
+		private System.Threading.Thread remuxThread;
+		private uint timeout;
+		private bool cancelled;
+		
+		public MpegRemuxer (string filepath)
+		{
+			this.filepath = filepath;
+			newFilepath = Path.ChangeExtension(filepath, "mp4");
+		}
+		
+		public string Remux(Window parent) {
+			Button cancellButton;
+			
+			/* Create the dialog */
+			dialog = new Dialog(Catalog.GetString("Remuxing file..."), parent, DialogFlags.Modal);
+			dialog.AllowGrow = false;
+			dialog.AllowShrink = false;
+			dialog.Deletable = false;
+			
+			/* Add a progress bar */
+			pb = new ProgressBar();
+			pb.Show();
+			dialog.VBox.Add(pb);
+			
+			/* Add a button to cancell the task */
+			cancellButton = new Button("gtk-cancel");
+			cancellButton.Clicked += OnStop; 
+			cancellButton.Show();
+			dialog.VBox.Add(cancellButton);
+			
+			/* Start the remux task in a separate thread */
+			remuxThread = new System.Threading.Thread(new ThreadStart(RemuxTask));
+			remuxThread.Start();
+			
+			/* Add a timeout to refresh the progress bar */ 
+			pb.Pulse();
+			timeout = GLib.Timeout.Add (1000, new GLib.TimeoutHandler (Update));
+			
+			/* Wait until the thread call Destroy on the dialog */
+			dialog.Run();
+			return cancelled ? null : newFilepath;
+		}
+		
+		private bool Update() {
+			pb.Pulse();			
+			return true;
+		}
+		
+		private void Stop() {
+			if (cancelled) {
+				if (remuxThread.IsAlive)
+					remuxThread.Interrupt();
+				File.Delete (newFilepath);
+			}
+			GLib.Source.Remove (timeout);
+			dialog.Destroy();
+		}
+		
+		private void RemuxTask(){
+			/* ffmpeg looks like the easiest and more accurate way to do the remux */
+			ProcessStartInfo startInfo = new ProcessStartInfo();
+			startInfo.CreateNoWindow = true;
+			startInfo.UseShellExecute = false;
+			startInfo.FileName = "ffmpeg";
+			startInfo.Arguments = String.Format("-i {0} -vcodec copy -acodec copy -y -sn {1} ",
+			                                    filepath, newFilepath);
+			using (System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(startInfo))
+			{
+				exeProcess.WaitForExit();
+				Application.Invoke (delegate {
+					Stop();
+				});
+			}
+		}
+		
+		private void OnStop (object sender, System.EventArgs args) {
+			cancelled = true;
+			Stop();
+		}
+		
+		public static bool FileIsMpeg(string filepath) {
+			string extension = Path.GetExtension(filepath).Replace(".", "").ToLower();
+			var extensions = new List<string> (MpegRemuxer.EXTENSIONS);
+			return extensions.Contains(extension);
+		}
+		
+		public static bool AskForConversion(Window parent) {
+			bool ret;
+			MessageDialog md = new MessageDialog(parent, DialogFlags.Modal, MessageType.Question,
+			                                     ButtonsType.YesNo,
+			                                     Catalog.GetString("The file you are trying to load is not properly " +
+			                                     	"supported. Would you like to convert it into a more suitable " +
+			                                     	"format?"));
+			md.TransientFor = parent;
+			ret = md.Run() == (int)ResponseType.Yes;
+			md.Destroy();
+			
+			return ret;
+		}
+	}
+}
+
diff --git a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
index c88c43d..507a603 100644
--- a/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
+++ b/LongoMatch/Gui/Component/ProjectDetailsWidget.cs
@@ -482,6 +482,15 @@ namespace LongoMatch.Gui.Component
 					MessageDialog md=null;
 					string filename = fChooser.Filename;
 					fChooser.Destroy();
+					
+					if (MpegRemuxer.FileIsMpeg(filename) &&
+					    MpegRemuxer.AskForConversion(this.Toplevel as Gtk.Window)) {
+						var remux = new MpegRemuxer(filename);
+						var newFilename = remux.Remux(this.Toplevel as Gtk.Window);
+						if (newFilename != null)
+							filename = newFilename;
+					}
+					
 					try {
 						md = new MessageDialog((Gtk.Window)this.Toplevel,
 						                       DialogFlags.Modal,
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 41d0f1d..7325f93 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -164,6 +164,7 @@ CesarPlayer/Utils/TimeString.cs
 CesarPlayer/Utils/PreviewMediaFile.cs
 CesarPlayer/Utils/FramesCapturer.cs
 CesarPlayer/Utils/MediaFile.cs
+CesarPlayer/Utils/MpegRemuxer.cs
 CesarPlayer/Common/Enum.cs
 CesarPlayer/Common/Handlers.cs
 CesarPlayer/Capturer/GstCameraCapturer.cs



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]