forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_subtitles.py
More file actions
62 lines (45 loc) · 1.62 KB
/
Copy pathsave_subtitles.py
File metadata and controls
62 lines (45 loc) · 1.62 KB
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
"""
As you can see, the subtitle API needs some work.
"""
import os
import sys
import pprint
from PIL import Image
from av import open
if not os.path.exists('subtitles'):
os.makedirs('subtitles')
video = open(sys.argv[1])
streams = [s for s in video.streams if s.type == b'subtitle']
if not streams:
print 'no subtitles'
exit(1)
print streams
count = 0
for pi, packet in enumerate(video.demux([streams[0]])):
print 'packet', pi
for si, subtitle in enumerate(packet.decode()):
print '\tsubtitle', si, subtitle
for ri, rect in enumerate(subtitle.rects):
if rect.type == 'ass':
print '\t\tass: ', rect, rect.ass.rstrip('\n')
if rect.type == 'text':
print '\t\ttext: ', rect, rect.text.rstrip('\n')
if rect.type == 'bitmap':
print '\t\tbitmap: ', rect, rect.width, rect.height, rect.pict_buffers
buffers = [b for b in rect.pict_buffers if b is not None]
if buffers:
imgs = [
Image.frombuffer('L', (rect.width, rect.height), buffer, "raw", "L", 0, 1)
for buffer in buffers
]
if len(imgs) == 1:
img = imgs[0]
elif len(imgs) == 2:
img = Image.merge('LA', imgs)
else:
img = Image.merge('RGBA', imgs)
img.save('subtitles/%04d.png' % count)
count += 1
if count > 10:
pass
# exit()