77from scipy .stats import entropy
88
99import musicalgestures
10- from musicalgestures ._utils import MgFigure , extract_wav , embed_audio_in_video , MgProgressbar , convert_to_avi , generate_outfilename
10+ from musicalgestures ._utils import MgFigure , extract_wav , embed_audio_in_video , MgProgressbar , convert_to_avi , generate_outfilename , ffmpeg_cmd
1111
1212
1313class Flow :
@@ -111,8 +111,10 @@ def dense(
111111 if not overwrite :
112112 target_name = generate_outfilename (target_name )
113113
114- fourcc = cv2 .VideoWriter_fourcc (* 'MJPG' )
115- out = cv2 .VideoWriter (target_name , fourcc , fps , (width , height ))
114+ cmd = ['ffmpeg' , '-y' , '-s' , '{}x{}' .format (width , height ),
115+ '-r' , str (fps ), '-f' , 'rawvideo' , '-pix_fmt' , 'bgr24' , '-vcodec' , 'rawvideo' ,
116+ '-i' , '-' , '-vcodec' , 'mjpeg' , '-q:v' , '3' , target_name ]
117+ out = ffmpeg_cmd (cmd , total_time = length , pipe = 'write' )
116118
117119 ret , frame1 = vidcap .read ()
118120 prev_frame = cv2 .cvtColor (cv2 .resize (frame1 , size ), cv2 .COLOR_BGR2GRAY )
@@ -154,14 +156,14 @@ def dense(
154156
155157 if skip_empty :
156158 if np .sum (rgb ) > 0 :
157- out .write (rgb .astype (np .uint8 ))
159+ out .stdin . write (rgb .astype (np .uint8 ))
158160 else :
159161 if ii == 0 :
160- out .write (rgb .astype (np .uint8 ))
162+ out .stdin . write (rgb .astype (np .uint8 ))
161163 else :
162- out .write (prev_rgb .astype (np .uint8 ))
164+ out .stdin . write (prev_rgb .astype (np .uint8 ))
163165 else :
164- out .write (rgb .astype (np .uint8 ))
166+ out .stdin . write (rgb .astype (np .uint8 ))
165167
166168 if skip_empty :
167169 if np .sum (rgb ) > 0 or ii == 0 :
@@ -232,7 +234,8 @@ def dense(
232234 return mgf
233235
234236 else :
235- out .release ()
237+ out .stdin .close ()
238+ out .wait ()
236239 destination_video = target_name
237240
238241 if self .has_audio :
@@ -321,7 +324,6 @@ def sparse(
321324
322325 vidcap = cv2 .VideoCapture (filename )
323326 ret , frame = vidcap .read ()
324- fourcc = cv2 .VideoWriter_fourcc (* 'MJPG' )
325327
326328 fps = int (vidcap .get (cv2 .CAP_PROP_FPS ))
327329 width = int (vidcap .get (cv2 .CAP_PROP_FRAME_WIDTH ))
@@ -336,7 +338,10 @@ def sparse(
336338 if not overwrite :
337339 target_name = generate_outfilename (target_name )
338340
339- out = cv2 .VideoWriter (target_name , fourcc , fps , (width , height ))
341+ cmd = ['ffmpeg' , '-y' , '-s' , '{}x{}' .format (width , height ),
342+ '-r' , str (fps ), '-f' , 'rawvideo' , '-pix_fmt' , 'bgr24' , '-vcodec' , 'rawvideo' ,
343+ '-i' , '-' , '-vcodec' , 'mjpeg' , '-q:v' , '3' , target_name ]
344+ out = ffmpeg_cmd (cmd , total_time = length , pipe = 'write' )
340345
341346 # params for ShiTomasi corner detection
342347 feature_params = dict (maxCorners = corner_max_corners ,
@@ -390,7 +395,7 @@ def sparse(
390395
391396 img = cv2 .add (frame , mask )
392397
393- out .write (img .astype (np .uint8 ))
398+ out .stdin . write (img .astype (np .uint8 ))
394399
395400 # Now update the previous frame and previous points
396401 old_gray = frame_gray .copy ()
@@ -403,7 +408,8 @@ def sparse(
403408 pb .progress (ii )
404409 ii += 1
405410
406- out .release ()
411+ out .stdin .close ()
412+ out .wait ()
407413
408414 destination_video = target_name
409415
0 commit comments