[xiph-commits] r16126 - in trunk/ffmpeg2theora: frontend frontend/theoraenc src

j at svn.xiph.org j at svn.xiph.org
Sun Jun 14 08:50:00 PDT 2009


Author: j
Date: 2009-06-14 08:49:59 -0700 (Sun, 14 Jun 2009)
New Revision: 16126

Modified:
   trunk/ffmpeg2theora/frontend/frontendmode.txt
   trunk/ffmpeg2theora/frontend/theoraenc/theoraenc.py
   trunk/ffmpeg2theora/src/ffmpeg2theora.c
   trunk/ffmpeg2theora/src/theorautils.c
Log:
chagne frontend mode, output a json dict, output to stdout by default, output duration with stats, not only once

Modified: trunk/ffmpeg2theora/frontend/frontendmode.txt
===================================================================
--- trunk/ffmpeg2theora/frontend/frontendmode.txt	2009-06-14 10:09:55 UTC (rev 16125)
+++ trunk/ffmpeg2theora/frontend/frontendmode.txt	2009-06-14 15:49:59 UTC (rev 16126)
@@ -1,3 +1,11 @@
+in frontend mode status is printed in json format and should be parsed
+one line at a time, example line:
+ {"duration": 28.953000, "position": 1.76, "audio_kbps":  59, "video_kbps": 292, "remaining": 15.45}
+last line indicates result, if all went well you get:
+ {"result": "ok"}
+if input could not be parsed at all you get:
+ {"result": "file does not exist or has unknown data format."}
+
 example in python to parse ffmpeg2theora in frontend mode:
 
 '''
@@ -2,17 +10,16 @@
 import os
+import simplejson
 
 cmd = "ffmpeg2theora  --frontend ..."
-f_stdout, f_stdin, f = os.popen3(cmd)
+f_stdout, f_stdin, f_stderr = os.popen3(cmd)
 info = {}
-line = f.readline()
+line = f_stdout.readline().strip()
 while line:
-    if line.startswith('f2t'):
-        for o in line.split(';')[1:]:
-            oo = o.split(': ')
-            if len(oo) >= 2:
-                info[oo[0]] = ": ".join(oo[1:]).strip()
+    try:
+        info = simplejson.loads(line)
 
-    #do something with info dict here
-
-    line = f.readline()
+        #do something with info dict here
+    except:
+        pass
+    line = f_stdout.readline().strip()
 '''

Modified: trunk/ffmpeg2theora/frontend/theoraenc/theoraenc.py
===================================================================
--- trunk/ffmpeg2theora/frontend/theoraenc/theoraenc.py	2009-06-14 10:09:55 UTC (rev 16125)
+++ trunk/ffmpeg2theora/frontend/theoraenc/theoraenc.py	2009-06-14 15:49:59 UTC (rev 16126)
@@ -8,6 +8,9 @@
 import signal
 import subprocess
 
+import simplejson
+
+
 resourcePath = abspath(dirname(__file__))
 
 def timestr(seconds):
@@ -65,18 +68,17 @@
   def encode(self):
     cmd = self.commandline()
     print cmd
-    p = subprocess.Popen(cmd, shell=False, stderr=subprocess.PIPE, close_fds=True)
+    p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, close_fds=True)
     self.p = p
-    f = p.stderr
+    f = p.stdout
     line = f.readline()
     info = dict()
     status = ''
     while line:
-      if line.startswith('f2t'):
-        for o in line.split(';')[1:]:
-          oo = o.split(': ')
-          if len(oo) >= 2:
-            info[oo[0]] = ": ".join(oo[1:]).strip()
+      try:
+        data = simplejson.loads(line)
+        for key in data:
+          info[key] = data[key]
         if 'position' in info:
           if 'duration' in info and float(info['duration']):
             encoded =  "encoding % 3d %% done " % ((float(info['position']) / float(info['duration'])) * 100)
@@ -89,6 +91,8 @@
         else:
           status = "encoding.."
         self.updateGUI(status)
+      except:
+        pass
       line = f.readline()
     f.close()
     if info.get('result', 'no') == 'ok':

Modified: trunk/ffmpeg2theora/src/ffmpeg2theora.c
===================================================================
--- trunk/ffmpeg2theora/src/ffmpeg2theora.c	2009-06-14 10:09:55 UTC (rev 16125)
+++ trunk/ffmpeg2theora/src/ffmpeg2theora.c	2009-06-14 15:49:59 UTC (rev 16126)
@@ -1813,7 +1813,7 @@
                             flag = -1;
                             break;
                         case FRONTEND_FLAG:
-                            info.frontend = stderr;
+                            info.frontend = stdout;
                             flag = -1;
                             break;
                         case FRONTENDFILE_FLAG:
@@ -2202,12 +2202,7 @@
                     exit(0);
                 }
 
-                if (info.frontend) {
-                    fprintf(info.frontend, "\nf2t ;duration: %f;\n", (float)convert->context->duration / AV_TIME_BASE);
-                    fflush(info.frontend);
-
-                }
-                else {
+                if (!info.frontend) {
                     dump_format(convert->context, 0,inputfile_name, 0);
                 }
                 if (convert->disable_audio) {
@@ -2227,20 +2222,20 @@
                     (double) convert->context->start_time / AV_TIME_BASE;
                 if (!info.outfile) {
                     if (info.frontend)
-                        fprintf(info.frontend, "\nf2t ;result: Unable to open output file.;\n");
+                        fprintf(info.frontend, "\"{result\": \"Unable to open output file.\"}\n");
                     else
                         fprintf(stderr,"\nUnable to open output file `%s'.\n", outputfile_name);
                     return(1);
                 }
                 if (convert->context->duration != AV_NOPTS_VALUE) {
-                    info.duration = convert->context->duration / AV_TIME_BASE;
+                    info.duration = (double)convert->context->duration / AV_TIME_BASE;
                 }
                 ff2theora_output(convert);
                 convert->audio_index =convert->video_index = -1;
             }
             else{
                 if (info.frontend)
-                    fprintf(info.frontend, "\nf2t ;result: input format not suported.;\n");
+                    fprintf(info.frontend, "{\"result\": \"input format not suported.\"}\n");
                 else
                     fprintf(stderr,"\nUnable to decode input.\n");
                 return(1);
@@ -2248,17 +2243,21 @@
             av_close_input_file(convert->context);
         }
         else{
-            fprintf(stderr, "\nFile `%s' does not exist or has an unknown data format.\n", inputfile_name);
+            if (info.frontend)
+                fprintf(info.frontend, "{\"result\": \"file does not exist or has unknown data format.\"}\n");
+            else
+                fprintf(stderr, "\nFile `%s' does not exist or has an unknown data format.\n", inputfile_name);
             return(1);
         }
     ff2theora_close(convert);
-    fprintf(stderr, "\n");
+    if (!info.frontend)
+        fprintf(stderr, "\n");
 
     if (*pidfile_name)
         unlink(pidfile_name);
 
     if (info.frontend)
-        fprintf(info.frontend, "\nf2t ;result: ok;\n");
+        fprintf(info.frontend, "{\"result\": \"ok\"}\n");
     if (info.frontend && info.frontend != stderr)
         fclose(info.frontend);
     return(0);

Modified: trunk/ffmpeg2theora/src/theorautils.c
===================================================================
--- trunk/ffmpeg2theora/src/theorautils.c	2009-06-14 10:09:55 UTC (rev 16125)
+++ trunk/ffmpeg2theora/src/theorautils.c	2009-06-14 15:49:59 UTC (rev 16126)
@@ -640,7 +640,8 @@
     int remaining_minutes = ((long) remaining / 60) % 60;
     int remaining_hours = (long) remaining / 3600;
     if (info->frontend) {
-        fprintf (info->frontend,"\nf2t ;position: %.02lf;audio_kbps: %d;video_kbps: %d;remaining: %.02lf\n",
+        fprintf(info->frontend, "{\"duration\": %lf, \"position\": %.02lf, \"audio_kbps\":  %d, \"video_kbps\": %d, \"remaining\": %.02lf}\n",
+            (double)info->duration,
             timebase,
             info->akbps, info->vkbps,
             remaining



More information about the commits mailing list