Guest User

drop_here_to_edit.py

a guest
Jul 3rd, 2021
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #v2
  2.  
  3. max_interval=180 # frames
  4. min_interval=max_interval-30
  5.  
  6. import subprocess
  7. import os
  8. import sys
  9.  
  10. if sys.argv[0]!="" and len(sys.argv)!=3:
  11.  input("Drag and drop image and video")
  12.  exit()
  13.  
  14. print(f"image: {sys.argv[1]}\nvideo: {sys.argv[2]}")
  15.  
  16.  
  17. face=sys.argv[1]
  18. video=sys.argv[2]
  19. dir=os.path.dirname(video)
  20. outdir=f"{dir}\\..\\output\\"
  21.  
  22. # retrieve frame data
  23. result = subprocess.run(['ffprobe', '-loglevel', 'error', '-select_streams', 'v:0', '-show_entries', 'frame=key_frame,coded_picture_number,pkt_pts_time', '-of', 'csv=print_section=0', video], stdout=subprocess.PIPE)
  24. fr=result.stdout.decode("utf-8").split("\r\n")
  25. fr=[x.split(",") for x in fr if x]
  26.  
  27. if len(set([x[2] for x in fr]))==1:
  28.  fr={i:[x[0],x[1]] for i,x in enumerate(fr)}
  29. else:
  30.  fr={int(x[2]):[x[0],x[1]] for x in fr}
  31.  
  32. # just do the editing if video is short
  33. if len(fr)<max_interval:
  34.  subprocess.run(['video.bat', face, video])
  35.  exit()
  36.  
  37. # keyframes
  38. kf=[x for x in fr if fr[x][0]=="1"]
  39. dif=[]
  40. y=None
  41. for x in kf:
  42.  if y!=None:
  43.   dif.append(x-y)
  44.  y=x
  45.  
  46.  
  47. video_s=os.path.splitext(video)
  48.  
  49. if max(dif)>max_interval or min(dif)<min_interval:
  50. # cut video into [interval]-sized slices
  51.  frames_left=len(fr)
  52.  f0=0
  53.  f1=0
  54.  i=0
  55.  while True:
  56.   f1=f0+max_interval if f0+max_interval<len(fr) else len(fr)-1
  57.   t=float(fr[f1][1])-float(fr[f0][1])
  58.   subprocess.run(['ffmpeg', '-i', video, '-loglevel', 'warning', '-stats', '-ss', fr[f0][1], '-t', str(t), video_s[0]+"_"+str(i)+video_s[1]], stdout=subprocess.PIPE)
  59.   i+=1
  60.   f0=f1
  61.   if f0+1>=len(fr):
  62.    break
  63. else:
  64. # cut video by keyframes if intervals are short enough (usually faster)
  65.  subprocess.run(['ffmpeg', '-i', video, '-loglevel', 'warning', '-stats', '-acodec', 'copy', '-f', 'segment', '-vcodec', 'copy', '-reset_timestamps', '1', '-map', '0', video_s[0]+"_%d."+video_s[1]], stdout=subprocess.PIPE)
  66.  i=len(kf)
  67.  
  68.  
  69. # edit sliced fragments
  70. videos=[f"{video_s[0]}_{str(x)}{video_s[1]}" for x in range(0,i)]
  71. edited_videos=[]
  72. for x in videos:
  73.  edited_videos.append(f"{outdir}{os.path.splitext(os.path.basename(face))[0]}_{os.path.basename(x)}")
  74.  subprocess.run(['video.bat', face, x])
  75.  
  76. # merge edited fragments
  77. list_video=os.path.join(dir, f"list_{os.path.basename(video)}.txt")
  78.  
  79. with open(list_video,"w") as f:
  80.  for x in edited_videos:
  81.   f.write(f"file '{x}'\n")
  82.  
  83. subprocess.run(['ffmpeg', '-f', 'concat', '-loglevel', 'warning', '-stats', '-safe', '0','-i', list_video, '-c', 'copy', f"{outdir}{os.path.splitext(os.path.basename(face))[0]}_{os.path.basename(video)}"], stdout=subprocess.PIPE)
  84.  
  85. # remove temp files
  86. os.remove(list_video)
  87. for x,y in zip(videos,edited_videos):
  88.  os.remove(x)
  89.  os.remove(y)
RAW Paste Data