@@ -957,7 +957,7 @@ def _stop(self, *args):
957
957
958
958
def save (self , filename , writer = None , fps = None , dpi = None , codec = None ,
959
959
bitrate = None , extra_args = None , metadata = None , extra_anim = None ,
960
- savefig_kwargs = None ):
960
+ savefig_kwargs = None , * , progress_callback = None ):
961
961
"""
962
962
Save the animation as a movie file by drawing every frame.
963
963
@@ -1013,6 +1013,22 @@ class to use, such as 'ffmpeg'. If ``None``, defaults to
1013
1013
on to the `savefig` command which is called repeatedly to
1014
1014
save the individual frames.
1015
1015
1016
+ progress_callback : function, optional
1017
+ A callback function that will be called for every frame to notify
1018
+ the saving progress. It must have the signature ::
1019
+
1020
+ def func(current_frame: int, total_frames: int) -> Any
1021
+
1022
+ where *current_frame* is the current frame number and
1023
+ *total_frames* is the total number of frames to be saved.
1024
+ *total_frames* is set to None, if the total number of frames can
1025
+ not be determined. Return values may exist but are ignored.
1026
+
1027
+ Example code to write the progress to stdout::
1028
+
1029
+ progress_callback =\
1030
+ lambda i, n: print(f'Saving frame {i} of {n}')
1031
+
1016
1032
Notes
1017
1033
-----
1018
1034
*fps*, *codec*, *bitrate*, *extra_args* and *metadata* are used to
@@ -1111,10 +1127,19 @@ class to use, such as 'ffmpeg'. If ``None``, defaults to
1111
1127
for anim in all_anim :
1112
1128
# Clear the initial frame
1113
1129
anim ._init_draw ()
1130
+ frame_number = 0
1131
+ save_count_list = [a .save_count for a in all_anim ]
1132
+ if None in save_count_list :
1133
+ total_frames = None
1134
+ else :
1135
+ total_frames = sum (save_count_list )
1114
1136
for data in zip (* [a .new_saved_frame_seq () for a in all_anim ]):
1115
1137
for anim , d in zip (all_anim , data ):
1116
1138
# TODO: See if turning off blit is really necessary
1117
1139
anim ._draw_next_frame (d , blit = False )
1140
+ if progress_callback is not None :
1141
+ progress_callback (frame_number , total_frames )
1142
+ frame_number += 1
1118
1143
writer .grab_frame (** savefig_kwargs )
1119
1144
1120
1145
# Reconnect signal for first draw if necessary
0 commit comments