LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

file output

Last updated at Posted at 2021-02-04

google colab file output part

youtube-vs-googletrans3.ipynb
translator = Translator()
num = 5
#obj_num = 1
filename = 'subtitle.txt'
with open(filename, 'w') as f:


  for count, l in enumerate(text_compo):
      if count + 1 < num:
          translated = translator.translate(l, dest='ja')
          print(count+1, l)
          linenum = f'{(count+1)}'
          f.write(linenum)
          f.write('  ')
          f.writelines(l)
          f.write('\n')
          print(count+1, translated.text)
          f.write(linenum)
          f.write('  ')
          f.writelines(translated.text)
          f.write('\n')
          f.write('\n')
      else:
          translated = translator.translate(l, dest='ja')
          print(count+1, l)
          linenum = f'{(count+1)}'
          f.write(linenum)
          f.write(linenum)
          f.write('  ')
          f.writelines(l)
          f.write('\n')
          print(count+1, translated.text)
          f.write(linenum)
          f.write('  ')      
          f.writelines(translated.text)
          f.write('\n')
          f.write('\n')
          del translator
          num = num + 5
          #obj_num = obj_num + 1
          #print("")
          #print("--- translator :", obj_num)
          #print("")
          translator = Translator()
  f.close()
files.download(filename)
del translator

stdout
https://stackabuse.com/writing-to-a-file-with-pythons-print-function/

google colab stdout to download file

youtube subtitle print only (not within gogletrans data)

youtube-caption-download
from youtube_transcript_api import YouTubeTranscriptApi
#from googletrans import Translator
from google.colab import files
#import time
import sys

video_id = 'Lbfe3-v7yE0' ## youtube video_id
line =[]
line[:] = YouTubeTranscriptApi.get_transcript(video_id,languages=['en'])

text_list = []
for l in line:
    ##print("text: ", l['text'])
    ##print("start:", l['start'])
    ##print("duration:", l['duration'])

    l['text']=l['text'].strip()
    l['text']=l['text'].rstrip('\n')
    l['text']=l['text'].replace('\n',' ')
    text_list.append(l['text'])
##print(line)    
del line
##print(text_list)

original_stdout = sys.stdout ## stdout backup
filename = 'subtitle.txt'
with open(filename, 'w') as f:
    sys.stdout = f # stdout to file

    print("haywhnk-A.K.A-@dauuricus")
    print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
    print("YouTube captions")
    print("- - - - - - - - - - - - - - - - - - -  YouTube  - - - - - - - - - - - - - - - - - - -")
    print()
    print()
    line = YouTubeTranscriptApi.list_transcripts(video_id)    

    transcript = line.find_transcript(['en'])
    #print(transcript.fetch())
    for count, dict_obj in enumerate(transcript.fetch()):
        print( count +1, dict_obj['text'] )
    print()
    print()
    print("************************************************************************************")
    print()
    print("Youtube captions")
    print("- - - - - - - - - - - - - - - - - - translated - - - - - - - - - - - - - - - - - - -")
    print()
    print()
    translated = transcript.translate('ja')
    for count, dict_obj in enumerate(translated.fetch()):
        print( count+1, dict_obj['text'] )
    print()
    print("-----------------------------------------------------------------------------------")
    print()
    print("captions text compositimg")
    print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
    print()
    print()
    ####line_list = []
    ####for l in text_list:
    ####    line_list.append(l.replace('\n',' '))
    ##text_list[:] = [a for a in text_list if a != ' ']
    ##text_list[:] = [l.replace('\n',' ') for l in text_list]


    text_compo = [] ##2 lines to 1 line
    i = 0
    txt = ''
    for count,l in enumerate(text_list):
        if i == 0:
          txt += l
          i = i + 1
          text_compo.append(txt)
        elif i == 1:
          txt += ' ' +l
          text_compo.pop()
          text_compo.append(txt)
          i = 0
          txt = ''
    print()
    print()
    print("************************************************************************************")
    print()
    print()
    for count, l in enumerate(text_list):
        print(count+1,l)
    print()
    print()
    print("************************************************************************************")
    print("  1/2 ")
    print()
    for count, l in enumerate(text_compo):
        print(count+1,l)
    print()
    print("************************************************************************************")
    print()
    print()
    print("************************************************************************************")
    print()
    print("Thank you.")

    sys.stdout = original_stdout

files.download(filename)
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up