class TestSBVReader(unittest.TestCase): def setUp(self): test_content = StringIO(u""" 0:00:03.490,0:00:07.430 >> FISHER: All right. So, let's begin. This session is: Going Social 0:00:07.430,0:00:11.600 with the YouTube APIs. I am Jeff Fisher, 0:00:11.600,0:00:14.009 and this is Johann Hartmann, we're presenting today. 0:00:14.009,0:00:15.889 [pause] """) self.reader = Reader(test_content) def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual(first.text, u">> FISHER: All right. So, let's begin.\nThis session is: Going Social\n") self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940))
class TestSBVWriter(unittest.TestCase): def setUp(self): test_content = StringIO(u""" 0:00:03.490,0:00:07.430 >> FISHER: All right. So, let's begin. This session is: Going Social 0:00:07.430,0:00:11.600 with the YouTube APIs. I am Jeff Fisher, 0:00:11.600,0:00:14.009 and this is Johann Hartmann, we're presenting today. 0:00:14.009,0:00:15.889 [pause] """) self.reader = Reader(test_content) self.writer = Writer(StringIO()) def test_transformtext(self): captions = self.reader.read() self.writer.captions = captions text = self.writer.captions_to_text() should_be = u"""00:00:03.490,00:00:07.430\n>> FISHER: All right. So, let's begin.\nThis session is: Going Social\n\n00:00:07.430,00:00:11.600\nwith the YouTube APIs. I am\nJeff Fisher,\n\n00:00:11.600,00:00:14.009\nand this is Johann Hartmann,\nwe're presenting today.\n\n00:00:14.009,00:00:15.889\n[pause]\n\n""" self.assertEqual(text, should_be) def test_format_time(self): caption = core.Caption() caption.start = core.get_date(second=3, millisecond=490) caption.end = core.get_date(second=7, millisecond=430) time_info = self.writer.format_time(caption) self.assertEqual(time_info['start'], '00:00:03.490') self.assertEqual(time_info['end'], '00:00:07.430')
class TestSBVReader(unittest.TestCase): def setUp(self): test_content = StringIO(u""" 0:00:03.490,0:00:07.430 >> FISHER: All right. So, let's begin. This session is: Going Social 0:00:07.430,0:00:11.600 with the YouTube APIs. I am Jeff Fisher, 0:00:11.600,0:00:14.009 and this is Johann Hartmann, we're presenting today. 0:00:14.009,0:00:15.889 [pause] """) self.reader = Reader(test_content) def test_read(self): captions = self.reader.read() self.assertTrue(captions is not None) self.assertEqual(len(captions), 4) first = captions[0] self.assertEqual(type(first.text), unicode) self.assertEqual( first.text, u">> FISHER: All right. So, let's begin.\nThis session is: Going Social\n" ) self.assertEqual(first.start, core.get_date(second=3, millisecond=490)) self.assertEqual(first.end, core.get_date(second=7, millisecond=430)) self.assertEqual(first.duration, timedelta(seconds=3, milliseconds=940))
def setUp(self): test_content = StringIO(u""" 0:00:03.490,0:00:07.430 >> FISHER: All right. So, let's begin. This session is: Going Social 0:00:07.430,0:00:11.600 with the YouTube APIs. I am Jeff Fisher, 0:00:11.600,0:00:14.009 and this is Johann Hartmann, we're presenting today. 0:00:14.009,0:00:15.889 [pause] """) self.reader = Reader(test_content)
of = f'{of[:(-5-len(str(counter - 1)))]}-{counter}{of[-4:]}' counter += 1 elif os.path.isfile(of): counter = 1 of = f'{of[:-4]}-{counter}{of[-4:]}' while os.path.isfile(of): of = f'{of[:(-5-len(str(counter - 1)))]}-{counter}{of[-4:]}' counter += 1 # Convert sbv to srt to add them if os.path.splitext(sf)[1] == '.sbv': from captionstransformer.sbv import Reader from captionstransformer.srt import Writer with open(sf) as r: reader = Reader(r) with open(f'{os.path.splitext(sf)[0]}.srt', 'w') as w: writer = Writer(w) writer.set_captions(reader.read()) writer.write() writer.close() print(f'Adding subtitles to {vf}...') if not args.soft_embed: subprocess.call(f'ffmpeg -hide_banner -loglevel warning -i "{vf}" -vf "subtitles=\'{sf}\':force_style=\'Fontsize={args.size},PrimaryColour=&H{args.color}&,BorderStyle={args.border_style}{args.additional_formatting}\'" -c:v {codec_string} -c:a copy "{of}"', cwd = os.getcwd(), shell = True) else: subprocess.call(f'ffmpeg -hide_banner -loglevel warning -i "{vf}" -i "{sf}" -c:v {codec_string} -c:a copy -c:s mov_text -disposition:s:0 default "{of}"', cwd = os.getcwd(), shell = True) # If the subtitle started off as sbv, try to delete the temporarily needed srt file