1+ # Copyright (c) 2024 bilive.
2+
3+ import argparse
4+ import os
5+ import subprocess
6+ import src .allconfig
7+ from src .burn .generate_danmakus import get_resolution , process_danmakus
8+ from src .burn .generate_subtitles import generate_subtitles
9+ from src .burn .render_video import render_video
10+
11+
12+ def normalize_video_path (filepath ):
13+ """Normalize the video path to upload
14+ Args:
15+ filepath: str, the path of video
16+ """
17+ parts = filepath .rsplit ('/' , 1 )[- 1 ].split ('_' )
18+ date_time_parts = parts [1 ].split ('-' )
19+ new_date_time = f"{ date_time_parts [0 ][:4 ]} -{ date_time_parts [0 ][4 :6 ]} -{ date_time_parts [0 ][6 :8 ]} -{ date_time_parts [1 ]} "
20+ return filepath .rsplit ('/' , 1 )[0 ] + '/' + parts [0 ] + '_' + new_date_time + '.mp4'
21+
22+ def merge_videos (in_final_video ):
23+ """Merge the video segments
24+ Args:
25+ in_final_video: str, the path of videos will be merged
26+ """
27+ merge_command = [
28+ 'ffmpeg' , '-f' , 'concat' , '-safe' , '0' , '-i' , 'mergevideo.txt' , '-use_wallclock_as_timestamps' , '1' ,
29+ '-c' , 'copy' , in_final_video
30+ ]
31+ with open (src .allconfig .MERGE_LOG_PATH , 'a' ) as mlog :
32+ subprocess .run (merge_command , stdout = mlog , stderr = subprocess .STDOUT )
33+ subprocess .run (['rm' , 'mergevideo.txt' ])
34+
35+ if __name__ == '__main__' :
36+ # Define the path to your file
37+ same_videos_list = src .allconfig .SRC_DIR + '/sameSegments.txt'
38+ output_video_path = ''
39+
40+ # Open the file and read it line by line
41+ with open (same_videos_list , 'r' ) as file :
42+ for line in file :
43+ # Strip whitespace from the line
44+ stripped_line = line .strip ()
45+ # Check if the line is not blank
46+ if stripped_line :
47+ directory = os .path .dirname (stripped_line )
48+ video_name = os .path .basename (stripped_line )
49+ tmp = directory + '/tmp/'
50+ if output_video_path == '' :
51+ output_video_path = normalize_video_path (stripped_line )
52+ print ("The output video is " + output_video_path )
53+ subprocess .run (['mkdir' , tmp ])
54+
55+ video_to_be_merged = tmp + video_name
56+ original_video_path = stripped_line
57+ xml_path = original_video_path [:- 4 ] + '.xml'
58+ ass_path = original_video_path [:- 4 ] + '.ass'
59+ srt_path = original_video_path [:- 4 ] + '.srt'
60+ jsonl_path = original_video_path [:- 4 ] + '.jsonl'
61+ # Recoginze the resolution of video
62+ video_resolution = get_resolution (original_video_path )
63+ # Process the danmakus to ass and remove emojis
64+ subtitle_font_size = process_danmakus (xml_path , video_resolution )
65+
66+ # Generate the srt file via whisper model
67+ if src .allconfig .GPU_EXIST :
68+ generate_subtitles (original_video_path )
69+
70+ # Burn danmaku or subtitles into the videos
71+ render_video (original_video_path , video_to_be_merged , subtitle_font_size )
72+ if not os .path .exists ('mergevideo.txt' ):
73+ open ('mergevideo.txt' , 'w' ).close ()
74+ with open ('mergevideo.txt' , 'a' ) as f :
75+ f .write (f"file '{ video_to_be_merged } '\n " )
76+ print ("complete danamku burning and wait for uploading!" )
77+
78+ for remove_path in [original_video_path , xml_path , ass_path , srt_path , jsonl_path ]:
79+ if os .path .exists (remove_path ):
80+ os .remove (remove_path )
81+
82+ # For test part
83+ # test_path = original_video_path[:-4]
84+ # os.rename(original_video_path, test_path)
85+
86+ subprocess .run (['rm' , same_videos_list ])
87+ merge_videos (output_video_path )
88+ subprocess .run (['rm' , '-r' , tmp ])
89+
90+ with open (f"{ src .allconfig .SRC_DIR } /uploadProcess/uploadVideoQueue.txt" , "a" ) as file :
91+ file .write (f"{ output_video_path } \n " )
0 commit comments