def test_detection(self): self.assertTrue(SRTReader().detect(SAMPLE_SRT.decode(u'utf-8')))
from pycaption import SRTReader, SAMIWriter import os dir = './srt' srtnames = [n for n in os.listdir(dir) if n[-4:] == '.srt'] for name in srtnames: puretxt = open(os.path.join(dir, name), 'r', encoding = 'utf-8').read() content = SRTReader().read(puretxt) smitext = SAMIWriter().write(content) print(smitext)
def test_detection(self, sample_srt): assert SRTReader().detect(sample_srt) is True
def test_caption_length(self): captions = SRTReader().read(SAMPLE_SRT) self.assertEqual(7, len(captions.get_captions(u"en-US")))
def test_numeric_captions(self): captions = SRTReader().read(SAMPLE_SRT_NUMERIC) self.assertEqual(7, len(captions.get_captions(u"en-US")))
def test_extra_empty_line(self): captions = SRTReader().read(SAMPLE_SRT_BLANK_LINES) self.assertEquals(2, len(captions.get_captions("en-US")))
def test_srt_to_microdvd_conversion(self, sample_microdvd, sample_srt): caption_set = SRTReader().read(sample_srt) results = MicroDVDWriter().write(caption_set) assert isinstance(results, str) self.assert_microdvd_equals(sample_microdvd, results)
def test_extra_trailing_empty_line(self): captions = SRTReader().read(SAMPLE_SRT_TRAILING_BLANKS) self.assertEqual(2, len(captions.get_captions("en-US")))
def test_srt_reader_only_supports_unicode_input(self): with self.assertRaises(InvalidInputError): SRTReader().read(b'')
def test_srt_to_sami_conversion(self): caption_set = SRTReader().read(SAMPLE_SRT) results = SAMIWriter().write(caption_set) self.assertTrue(isinstance(results, unicode)) self.assertSAMIEquals(SAMPLE_SAMI, results)
def test_srt_to_webvtt_conversion(self): caption_set = SRTReader().read(SAMPLE_SRT) results = WebVTTWriter().write(caption_set) self.assertTrue(isinstance(results, unicode)) self.assertWebVTTEquals(SAMPLE_WEBVTT_FROM_SRT, results)
def test_srt_to_srt_conversion(self): caption_set = SRTReader().read(SAMPLE_SRT) results = SRTWriter().write(caption_set) self.assertTrue(isinstance(results, six.text_type)) self.assertSRTEquals(SAMPLE_SRT, results)
import os import sys import codecs import struct import wave from pycaption import SRTReader framerate = 24000 content = codecs.open('output.srt', 'r', 'utf-8').read() srt = SRTReader().read(content, lang='en') caps = srt.get_captions('en') out = wave.open('output.wav', 'w') out.setnchannels(1) out.setsampwidth(2) out.setframerate(framerate) outnframes = 0 for i, cap in enumerate(caps): start = cap.start / 1000000.0 end = cap.end / 1000000.0 nframesToFill = int((end - start) * framerate) wav = wave.open(os.path.join('data_aljazeera', '%d.wav' % (i + 1)), 'r') nframes = wav.getnframes() out.writeframes(wav.readframes(nframes)) wav.close() # need to fill nframesToFill - nframes zero frames for j in xrange(nframesToFill - nframes): out.writeframes(struct.pack('h', 0)) outnframes += nframesToFill
) parser.add_argument( "--skip-first", action="store_true", default=False, help="If it is True, the first caption will be skipped.") parser.add_argument( "--break-after", type=float, default=0, help="Enable test mode to finish after the given seconds.") parser.add_argument("--output-params", type=str) args = parser.parse_args() srt_text = open(args.srt).read() srt = SRTReader().read(srt_text, lang="en") captions = srt.get_captions("en") reader = imageio.get_reader(args.video) meta_data = reader.get_meta_data() pprint.pprint(meta_data) fps = meta_data["fps"] if args.output_params: output_params = shlex.split(args.output_params) else: output_params = [] print("save to:", args.out) writer = imageio.get_writer( args.out,
def test_caption_length(self): captions = SRTReader().read(SAMPLE_SRT.decode(u'utf-8')) self.assertEquals(8, len(captions.get_captions(u"en-US")))
def test_caption_length(self, sample_srt): captions = SRTReader().read(sample_srt) assert 7 == len(captions.get_captions("en-US"))
def test_numeric_captions(self): captions = SRTReader().read(SAMPLE_SRT_NUMERIC.decode(u'utf-8')) self.assertEquals(7, len(captions.get_captions(u"en-US")))
def test_proper_timestamps(self, sample_srt): captions = SRTReader().read(sample_srt) paragraph = captions.get_captions("en-US")[2] assert 17000000 == paragraph.start assert 18752000 == paragraph.end
def test_srt_to_sami_conversion(self, sample_sami, sample_srt): caption_set = SRTReader().read(sample_srt) results = SAMIWriter().write(caption_set) assert isinstance(results, str) self.assert_sami_equals(sample_sami, results)
def test_numeric_captions(self, sample_srt_numeric): captions = SRTReader().read(sample_srt_numeric) assert 7 == len(captions.get_captions("en-US"))
def test_detection(self): self.assertTrue(SRTReader().detect(SAMPLE_SRT))
def test_empty_file(self, sample_srt_empty): with pytest.raises(CaptionReadNoCaptions): SRTReader().read(sample_srt_empty)
def test_proper_timestamps(self): captions = SRTReader().read(SAMPLE_SRT) paragraph = captions.get_captions(u"en-US")[2] self.assertEqual(17000000, paragraph.start) self.assertEqual(18752000, paragraph.end)
def test_extra_trailing_empty_line(self, sample_srt_trailing_blanks): captions = SRTReader().read(sample_srt_trailing_blanks) assert 2 == len(captions.get_captions("en-US"))
def test_empty_file(self): self.assertRaises( CaptionReadNoCaptions, SRTReader().read, SAMPLE_SRT_EMPTY)
def build_srt_reader(): return SubtitleReader(SRTReader(), requires_language=True)