summaryrefslogtreecommitdiff
path: root/soundconverter/batch.py
blob: 8c9eabd557ce0b19f522486e3b7527e8663d9b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# SoundConverter - GNOME application for converting between audio formats.
# Copyright 2004 Lars Wirzenius
# Copyright 2005-2012 Gautier Portet
#
# 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; version 3 of the License.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA


import sys
import gobject
import time
from soundfile import SoundFile
import error
from soundconverter.settings import settings
from gstreamer import TagReader
from namegenerator import TargetNameGenerator
from queue import TaskQueue
from gstreamer import Converter
from fileoperations import unquote_filename

def cli_tags_main(input_files):
    error.set_error_handler(error.ErrorPrinter())
    loop = gobject.MainLoop()
    gobject.threads_init()
    context = loop.get_context()
    for input_file in input_files:
        input_file = SoundFile(input_file)
        if not settings['quiet']:
            print(input_file.filename)
        t = TagReader(input_file)
        t.start()
        while t.running:
            time.sleep(0.01)
            context.iteration(True)
            
        if not settings['quiet']:
            for key in sorted(input_file.tags):
                print('     %s: %s' % (key, input_file.tags[key]))


class CliProgress:

    def __init__(self):
        self.current_text = ''

    def show(self, new_text):
        if new_text != self.current_text:
            self.clear()
            sys.stdout.write(new_text)
            sys.stdout.flush()
            self.current_text = new_text

    def clear(self):
        sys.stdout.write('\b \b' * len(self.current_text))
        sys.stdout.flush()


def cli_convert_main(input_files):
    loop = gobject.MainLoop()
    gobject.threads_init()
    context = loop.get_context()
    error.set_error_handler(error.ErrorPrinter())

    output_type = settings['cli-output-type']
    output_suffix = settings['cli-output-suffix']

    generator = TargetNameGenerator()
    generator.suffix = output_suffix

    progress = CliProgress()

    queue = TaskQueue()
    for input_file in input_files:
        input_file = SoundFile(input_file)
        output_name = generator.get_target_name(input_file)
        c = Converter(input_file, output_name, output_type)
        c.init()
        c.start()
        while c.running:
            if c.get_duration():
                percent = min(100, 100.0* (c.get_position() / c.get_duration()))
                percent = '%.1f %%' % percent
            else:
                percent = '/-\|' [int(time.time()) % 4]
            progress.show('%s: %s' % (unquote_filename(c.sound_file.filename[-65:]), percent ))
            time.sleep(0.01)
            context.iteration(True)
        print

    previous_filename = None
    
    '''
    queue.start()
    
    #running, progress = queue.get_progress(perfile)
    while queue.running:
        t = None #queue.get_current_task()
        if t and not settings['quiet']:
            if previous_filename != t.sound_file.get_filename_for_display():
                if previous_filename:
                    print _('%s: OK') % previous_filename
                previous_filename = t.sound_file.get_filename_for_display()

            percent = 0
            if t.get_duration():
                percent = '%.1f %%' % ( 100.0* (t.get_position() / t.get_duration() ))
            else:
                percent = '/-\|' [int(time.time()) % 4]
            progress.show('%s: %s' % (t.sound_file.get_filename_for_display()[-65:], percent ))
        time.sleep(0.10)
        context.iteration(True)
    '''
    if not settings['quiet']:
        progress.clear()