Guest User

Untitled

a guest
Mar 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import os, subprocess, json, shutil
  2.  
  3. PATH_TO = '/path/to/files/' # директория с видео
  4. NUMBER_OF_FRAMES = 20 # количество скриншотов
  5.  
  6. def create_snapshots(filename):
  7.  
  8. print('Create snapshot to '+filename)
  9.  
  10. #get video duration from FFprobe
  11. data = subprocess.Popen(
  12. ["ffprobe", "-v", "error", "-print_format", "json", "-show_entries", "format=duration", PATH_TO+filename],
  13. stdout=subprocess.PIPE
  14. ).communicate()
  15.  
  16. duration = json.loads(data[0].decode("utf-8"))['format']['duration']
  17.  
  18. print('duration in sec: '+str(duration))
  19. now = round(float(duration))
  20. print('duration in format: '+str(now // 3600) + ':' + str(now // 60 % 60) + ':' + str(now % 60))
  21.  
  22. interval = round(float(duration)/NUMBER_OF_FRAMES)-1
  23. print('interval: '+str(interval))
  24.  
  25. #make snapshots folder
  26. tmp_v = PATH_TO+str(filename)+'s'
  27. if not os.path.exists(tmp_v):
  28. os.mkdir(tmp_v)
  29.  
  30. for i in range(NUMBER_OF_FRAMES):
  31. now = interval*(i+1)
  32. time = str(now // 3600) + ':' + str(now // 60 % 60) + ':' + str(now % 60)
  33. FFMPEG_SNAPSHOT_COMMAND = 'ffmpeg -ss '+time+' -i '+PATH_TO+filename+' -vframes 1 -q:v 2 '+PATH_TO+filename+'s/'+str(i+1)+'.jpg -y'
  34. try:
  35. os.system(FFMPEG_SNAPSHOT_COMMAND)
  36. print('Build '+str(i+1)+'.jpg: OK')
  37. except:
  38. print('Build '+str(i+1)+'.jpg: ERROR')
Add Comment
Please, Sign In to add comment