spacepaste

  1.  
  2. import os
  3. import subprocess
  4. def freezeLastFrame(tmpdir, fVideoStream, freeze_s, fVideoStreamOut):
  5. try:
  6. # import subprocess, os
  7. E = os.environ.copy()
  8. E.update({
  9. 'TMP': tmpdir,
  10. 'SRC': fVideoStream,
  11. 'FREEZE_S': str(freeze_s),
  12. 'DST': fVideoStreamOut
  13. })
  14. cmd = '''
  15. cd "$TMP"
  16. FFMPEG="ffmpeg -y -hide_banner -nostats"
  17. # get last frame
  18. NFRAMES=`ffprobe -show_streams "$SRC" 2> /dev/null | grep nb_frames | head -1 | cut -d \= -f 2`
  19. let "LAST_FRAME=$NFRAMES-1"
  20. $FFMPEG -i $SRC -vf select=\\'eq\(n,$LAST_FRAME\) -vframes 1 LAST_FRAME.jpg
  21. # make last-frame-frozen video
  22. FRAMERATE=`ffprobe -v 0 -of csv=p=0 -select_streams 0 -show_entries stream=r_frame_rate "$SRC"`
  23. $FFMPEG -loop 1 -framerate 1 -i LAST_FRAME.jpg -r "$FRAMERATE" -t "$FREEZE_S" LAST_FRAME.mp4 # -pix_fmt yuv420p
  24. # Produces list.txt like this:
  25. # file foo.mp4
  26. # file 'bar with spaces.mp4'
  27. printf "file %q\\n" "$SRC" LAST_FRAME.mp4 > list.txt
  28. # concatenate
  29. # $FFMPEG -f concat -safe 0 -i list.txt -c copy "$DST" # problem: sometimes get green-screen for LAST_FRAME.mp4 duration
  30. $FFMPEG -i "$SRC" -i LAST_FRAME.mp4 -filter_complex "[0:v] [1:v] concat=n=2:v=1 [v]" -map "[v]" "$DST"
  31. # from https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
  32. '''
  33. # subprocess.Popen( cmd, env=E )
  34. subprocess.check_output(cmd, shell=True, env=E, executable="/bin/bash")
  35. except subprocess.CalledProcessError as e:
  36. print('freezeLastFrameIfVideoShorter Error:' + str(e))
  37. except:
  38. print('freezeLastFrameIfVideoShorter unknown Error!')
  39. return fVideoStreamOut
  40. freezeLastFrame(tmpdir='.', fVideoStream='./SampleVideo_1280x720_1mb.mp4', freeze_s=3, fVideoStreamOut='out.mp4')
  41.