Commit a320d7e9 authored by Jan Gerber's avatar Jan Gerber
Browse files

chagne frontend mode, output a json dict, output to stdout by default, output...

chagne frontend mode, output a json dict, output to stdout by default, output duration with stats, not only once
parent 3e002371
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:
'''
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()
#do something with info dict here
try:
info = simplejson.loads(line)
line = f.readline()
#do something with info dict here
except:
pass
line = f_stdout.readline().strip()
'''
......@@ -8,6 +8,9 @@ import sys
import signal
import subprocess
import simplejson
resourcePath = abspath(dirname(__file__))
def timestr(seconds):
......@@ -65,18 +68,17 @@ class TheoraEnc:
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 @@ class TheoraEnc:
else:
status = "encoding.."
self.updateGUI(status)
except:
pass
line = f.readline()
f.close()
if info.get('result', 'no') == 'ok':
......
......@@ -1813,7 +1813,7 @@ int main(int argc, char **argv) {
flag = -1;
break;
case FRONTEND_FLAG:
info.frontend = stderr;
info.frontend = stdout;
flag = -1;
break;
case FRONTENDFILE_FLAG:
......@@ -2202,12 +2202,7 @@ int main(int argc, char **argv) {
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 @@ int main(int argc, char **argv) {
(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 @@ int main(int argc, char **argv) {
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);
......
......@@ -640,7 +640,8 @@ static void print_stats(oggmux_info *info, double timebase) {
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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment