File: enc2.py

package info (click to toggle)
pyvorbis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 304 kB
  • ctags: 268
  • sloc: ansic: 2,424; python: 530; sh: 335; makefile: 63
file content (48 lines) | stat: -rwxr-xr-x 1,128 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
'''An example of encoding using the Python wave module'''
import ogg.vorbis, wave

fout = open('out.ogg', 'wb')
inwav = wave.open('in.wav','rb')
channels = inwav.getnchannels()
vd = ogg.vorbis.VorbisInfo(channels = channels,
                           rate = inwav.getframerate(),
                           quality = 0.2).analysis_init()
os = ogg.OggStreamState(5)
map(os.packetin, vd.headerout())
og = os.flush()
while og:
    og.writeout(fout)
    og = os.flush()
nsamples = 1024

eos = 0
total = 0
while not eos:
    data = inwav.readframes(nsamples)
    total = total + nsamples
    if not data:
        vd.write(None)
        break
    vd.write_wav(data)
    #print 100.0 * total / inwav.getnframes()
    vb = vd.blockout()
    while vb:
        vb.analysis()
        vb.addblock()

        op = vd.bitrate_flushpacket()
        while op:
            os.packetin(op)
            while not eos:
                og = os.pageout()
                if not og: break
                og.writeout(fout)
                eos = og.eos()
            op = vd.bitrate_flushpacket()
        vb = vd.blockout()