Writes the given list of scenes to an output file handle in CSV format. Arguments: output_csv_file: Handle to open file in write mode. scene_list: List of pairs of FrameTimecodes denoting each scene's start/end FrameTimecode. include_cut_list: Bool indicating if the firs
(
output_csv_file: ty.TextIO,
scene_list: SceneList,
include_cut_list: bool = True,
cut_list: CutList | None = None,
col_separator: str = ",",
row_separator: str = "\n",
)
| 55 | |
| 56 | |
| 57 | def write_scene_list( |
| 58 | output_csv_file: ty.TextIO, |
| 59 | scene_list: SceneList, |
| 60 | include_cut_list: bool = True, |
| 61 | cut_list: CutList | None = None, |
| 62 | col_separator: str = ",", |
| 63 | row_separator: str = "\n", |
| 64 | ): |
| 65 | """Writes the given list of scenes to an output file handle in CSV format. |
| 66 | |
| 67 | Arguments: |
| 68 | output_csv_file: Handle to open file in write mode. |
| 69 | scene_list: List of pairs of FrameTimecodes denoting each scene's start/end FrameTimecode. |
| 70 | include_cut_list: Bool indicating if the first row should include the timecodes where |
| 71 | each scene starts. Should be set to False if RFC 4180 compliant CSV output is required. |
| 72 | cut_list: Optional list of FrameTimecode objects denoting the cut list (i.e. the frames |
| 73 | in the video that need to be split to generate individual scenes). If not specified, |
| 74 | the cut list is generated using the start times of each scene following the first one. |
| 75 | col_separator: Delimiter to use between values. Must be single character. |
| 76 | row_separator: Line terminator to use between rows. |
| 77 | |
| 78 | Raises: |
| 79 | TypeError: "delimiter" must be a 1-character string |
| 80 | """ |
| 81 | csv_writer = csv.writer(output_csv_file, delimiter=col_separator, lineterminator=row_separator) |
| 82 | # If required, output the cutting list as the first row (i.e. before the header row). |
| 83 | if include_cut_list: |
| 84 | csv_writer.writerow( |
| 85 | ["Timecode List:", *cut_list] |
| 86 | if cut_list |
| 87 | else [start.get_timecode() for start, _ in scene_list[1:]] |
| 88 | ) |
| 89 | csv_writer.writerow( |
| 90 | [ |
| 91 | "Scene Number", |
| 92 | "Start Frame", |
| 93 | "Start Timecode", |
| 94 | "Start Time (seconds)", |
| 95 | "End Frame", |
| 96 | "End Timecode", |
| 97 | "End Time (seconds)", |
| 98 | "Length (frames)", |
| 99 | "Length (timecode)", |
| 100 | "Length (seconds)", |
| 101 | ] |
| 102 | ) |
| 103 | for i, (start, end) in enumerate(scene_list): |
| 104 | duration = end - start |
| 105 | csv_writer.writerow( |
| 106 | [ |
| 107 | f"{i + 1:d}", |
| 108 | f"{start.frame_num + 1:d}", |
| 109 | start.get_timecode(), |
| 110 | f"{start.seconds:.3f}", |
| 111 | f"{end.frame_num:d}", |
| 112 | end.get_timecode(), |
| 113 | f"{end.seconds:.3f}", |
| 114 | f"{duration.frame_num:d}", |