PyUGAT

Nächstes Treffen der Study Group für Anfängerinnen und Anfänger: Dienstag 14. 8. 2019

Python gilt als leicht verständich, gut zu erlernen und eine ausgezeichnete Sprache zum Einstieg ins Programmieren.

Im Mittelpunkt dieser Gruppe sollen Learning-by-Doing, gemeinsames Ausarbeiten von Problemen und gegenseitiges Helfen stehen.

Herzlich eingeladen sind alle, die gerade angefangen haben oder anfangen möchten, sich mit Python oder dem Programmieren zu beschäftigen

Die Treffen werden auch auf diversen Webseiten und Mailinglisten angekündigt.

Aufgabe für dieses mal;

last python study group we did a theoretical lesson - so we will do a coding session this time. During next lesson on 14th of August I have an optional project for you (this is the project I am working on at the moment) - if you want, you can code something for this project or otherwise we would ask you to work at your own project in python - we will surely have the time to look at your project or what you've learned together surely everyone will have the opportunity to ask their questions to the senior python-team.

Find my example here: https://bitbucket.org/stormtrooper02/dvd-backup/src/master/

The project is around ripping dvd's... I want to store my dvd's at my homeserver - so watching DVD's is possible without DVD-Player - I would like to ask you to think about the best way to realize it until next week - you also can start implementing at home.

Prerequisite you have:
OUTPUTFILE.chapters

metadata.txt

This is Adi's solution:

   1 dir = '/home/adi/Videos/'
   2 chaptersfile = 'Red_Heat.chapters'
   3 film_title = 'Red Heat'
   4 outputfile = chaptersfile.rsplit('.', 1)[0] + '.metadata'
   5 time_offset = 36     # if you want to cut the first 36 seconds of the video away the chapter marks have to be earlier
   6 
   7 '''
   8 This program prepares you a metadata file for ripping DVD's with ffmpeg.
   9 You need to extract the chapter information in the DVD with the command 'mplayer -identify dvd://1' before processing the data.
  10 '''
  11 
  12 
  13 def read_chapterfile():
  14     chaptermarks = ''
  15     total_secs = ''
  16     with open(dir + chaptersfile, 'r', encoding='utf-8') as originalfile:
  17         content = originalfile.readlines()
  18         for line in content:
  19             if line.startswith('CHAPTERS: '):
  20                 chaptermarks = line[10:-2].split(',')
  21             if line.startswith('ID_LENGTH'):
  22                 total_secs = float(line[10:])
  23 
  24         end_time = "{0:02d}:{1:02d}:{2:06.3f}".format(int(total_secs / 3600), int(total_secs % 3600 / 60),
  25                                                       total_secs % 3600 % 60)
  26         chaptermarks.append(end_time)
  27         return chaptermarks
  28 
  29 
  30 if __name__ == '__main__':
  31     chaptermarks = read_chapterfile()
  32 
  33     oldtime = None
  34     newtime = None
  35 
  36     with open(dir + outputfile, 'w') as mytxtfile:
  37         mytxtfile.write(";FFMETADATA1\n")
  38         mytxtfile.write("title={0}\n".format(film_title))
  39         mytxtfile.write(";begin film encoding at second {0} with 'ffmpeg -ss 00:00:{0:02d}'\n".format(time_offset))
  40         mytxtfile.close()
  41 
  42     for i in range(len(chaptermarks)):
  43         oldtime = newtime
  44 
  45         if oldtime == None:
  46             timelist_str = chaptermarks[i].split(':')
  47             timelist_float = [float(i) for i in timelist_str]
  48             hours, minutes, seconds = timelist_float
  49             newtime = hours * 60 * 60 + minutes * 60 + seconds
  50             continue
  51 
  52         timelist_str = chaptermarks[i].split(':')
  53         timelist_float = [float(i) for i in timelist_str]
  54         hours, minutes, seconds = timelist_float
  55         newtime = hours * 60 * 60 + minutes * 60 + seconds - time_offset
  56         #    print('{0} -> {1:06.3f}'.format(oldtime,newtime))
  57 
  58         with open(dir + outputfile, 'a') as mytxtfile:
  59             string = '\n[CHAPTER]\n' \
  60                      'TIMEBASE=1/1\n' \
  61                      'START={0}\n' \
  62                      'END={1}\n' \
  63                      'title= {2} / chapter {3:02d}\n'.format(oldtime, newtime, chaptermarks[i - 1].rsplit('.', 1)[0], i)
  64             mytxtfile.write(string)

This is Wayne's solution:

   1 with open("chapters.txt") as f:
   2     content = f.readlines ()
   3 
   4 allTimes = []
   5 MaxLength = 0
   6 
   7 for index,line in enumerate(content):
   8     if line.startswith('ID_LENGTH'):
   9         MaxLength = float(line[10:-2])
  10 
  11     if line.startswith('CHAPTER'):
  12         chapters = line[10:-2].split(',')
  13         for chapterTime in chapters:
  14             hours, minutes, seconds = [float(i) for i in chapterTime.split(':')]
  15             seconds = seconds + minutes * 60 + hours * 60 * 60
  16             allTimes.append(seconds)
  17 
  18 allTimes.append(MaxLength)
  19 FileString = ';FFMETADATA1\n\n'
  20 
  21 for idx in range(len(allTimes)-1):
  22     FileString += f'''
  23 [CHAPTER]
  24 TIMEBASE=1/1
  25 START={allTimes[idx]:.2f}
  26 END={allTimes[idx+1]:.2f}
  27 title=chapter #{idx+1}
  28 
  29 '''
  30 with open("meta.txt",'w') as metafile:
  31     metafile.write(FileString)
  32 
  33 #     TLDR;
  34 # camelCase  
  35 # PascalCase // class
  36 # snake_case // variable ,function
  37 # kebab-case


CategoryShownotes

PyUGAT: Treffen/2019-08-Studygroup (last edited 2019-08-16 10:40:44 by Adi)