text: import os
  
  
  DATA_DIR = "../../../data/ava_kinetics/annotations"
  
  # download annotations and extrac to the data folder
  def download_annotations(data_dir=DATA_DIR):    
      os.system('mkdir -p ' + data_dir)
      os.system('wget https://storage.googleapis.com/deepmind-media/'
                  'Datasets/ava_kinetics_v1_0.tar.gz')
      os.system('tar xf ava_kinetics_v1_0.tar.gz -C ' + data_dir)
      os.system('rm ava_kinetics_v1_0.tar.gz')
      return
  
  
  # collect the covered timestamps for involved videos
  def compute_covered_timestamps(data_dir=DATA_DIR, buffer_steps=5):
      root = DATA_DIR + '/ava_kinetics_v1_0/'
      key_frames = {}
      for split in 'train', 'val':
          kinetics_records = open(root + 'kinetics_%s_v1.0.csv'%split).readlines()
          for line in kinetics_records:
              line = line.split(',')
              if len(line) == 7:
                  vid, time = line[:2]
                  time = int(float(time))
                  if vid not in key_frames:
                      key_frames[vid] = [time]
                  else:
                      key_frames[vid] += [time]
  
      for vid in key_frames:
          start = min(key_frames[vid]) - buffer_steps
          start = max(start, 0)
          end = max(key_frames[vid]) + buffer_steps
          key_frames[vid] = [start, end]
  
      covered_intervals = [key_frames[vid][1]-key_frames[vid][0] for vid in key_frames]
      ava_length = sum(covered_intervals) / len(covered_intervals)
      print('The Kinetics part has %d videos.'%len(key_frames))
      print('The average length of the Kinetics part is %.2f seconds.'%ava_length)
  
      start_end_file = DATA_DIR + '/kinetics_timestamps.txt'
      f = open(start_end_file, 'w')
      for vid in key_frames:
          string = '%s,%d,%d\n'%(vid, key_frames[vid][0], key_frames[vid][1])
          f.write(string)
      f.close()
      return
  
  
  if __name__ == '__main__':
      download_annotations()
      compute_covered_timestamps(buffer_steps=5)
  