Skip to content

Commit 152b7c3

Browse files
committed
concatenate only good video chunks
mix together
1 parent a381c65 commit 152b7c3

File tree

2 files changed

+59
-41
lines changed

2 files changed

+59
-41
lines changed

main.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tools.audio_splitter_ffmpeg import *
44
from tools.video_downloader import *
55
from tools.video_editing import *
6+
import sys
67

78
if __name__ == '__main__':
89
# Prepare folders
@@ -12,19 +13,23 @@
1213
os.makedirs('final_output', exist_ok=True)
1314
os.makedirs('logs', exist_ok=True)
1415

16+
# Redirect all print outputs to a log file
17+
# log_output_file = open('logs/log.txt', 'w')
18+
# sys.stdout = log_output_file
19+
1520
# Set the multiprocessing start method to 'spawn'
1621
multiprocessing.set_start_method('spawn')
1722

1823
# Download video
19-
# url = input("Enter YouTube URL: ")
20-
# video = video_download(url)
21-
video = "downloads/xSh7PuWAxXU.mp4"
24+
url = input("Enter YouTube URL: ")
25+
video = video_download(url)
26+
# video = "downloads/xSh7PuWAxXU.mp4"
2227

2328
# # Extract audio
24-
# audio = audio_extractor(video)
29+
audio = audio_extractor(video)
2530

2631
## Load audio
27-
audio = 'original_audios/xSh7PuWAxXU.wav'
32+
# audio = 'original_audios/xSh7PuWAxXU.wav'
2833
original_audio_name = os.path.splitext(os.path.basename(audio))[0]
2934

3035
## Transcribe audio
@@ -36,32 +41,32 @@
3641
print("Audio split")
3742

3843
## Translate each segment
39-
# new_segments = []
40-
# i = 0
41-
# total_segments = len(segments)
42-
# for segment in segments:
43-
# if len(segment['text']) > 0:
44-
# translated_text = translate_deepl(segment['text'], 'es', detected_language)
45-
# else:
46-
# translated_text = ''
47-
# new_segments.append({'id': segment['id'],
48-
# 'seek': segment['seek'],
49-
# 'start': segment['start'],
50-
# 'end': segment['end'],
51-
# 'text': translated_text
52-
# })
53-
# percentage = (i+1)/total_segments*100
54-
# # Print the progress
55-
# print(f'Translation progress: {percentage:.2f}%')
56-
# i += 1
57-
# print(f"Audio translated")
44+
new_segments = []
45+
i = 0
46+
total_segments = len(segments)
47+
for segment in segments:
48+
if len(segment['text']) > 0:
49+
translated_text = translate_deepl(segment['text'], 'es', detected_language)
50+
else:
51+
translated_text = ''
52+
new_segments.append({'id': segment['id'],
53+
'seek': segment['seek'],
54+
'start': segment['start'],
55+
'end': segment['end'],
56+
'text': translated_text
57+
})
58+
percentage = (i+1)/total_segments*100
59+
# Print the progress
60+
print(f'Translation progress: {percentage:.2f}%')
61+
i += 1
62+
print(f"Audio translated")
5863

5964
## save new_segments as a pickle for later loading
60-
# import pickle
65+
import pickle
6166

62-
# with open('new_segments.pkl', 'wb') as f:
63-
# pickle.dump(new_segments, f)
64-
# print("New segments saved")
67+
with open('new_segments.pkl', 'wb') as f:
68+
pickle.dump(new_segments, f)
69+
print("New segments saved")
6570

6671
## Load new_segments from pickle
6772
import pickle
@@ -103,13 +108,17 @@
103108
)
104109

105110
# Delete all content in output_audio folder
106-
# for filename in os.listdir('output_audio'):
107-
# file_path = os.path.join('output_audio', filename)
108-
# if os.path.isfile(file_path):
109-
# os.remove(file_path)
111+
for filename in os.listdir('output_audio'):
112+
file_path = os.path.join('output_audio', filename)
113+
if os.path.isfile(file_path):
114+
os.remove(file_path)
110115

111116

112117
# Mix old video file and new audio file
113118
# video_path = f'downloads/{original_audio_name}.mp4'
114119
# audio_path = f'final_output/{original_audio_name}-{target_lang_code}.wav'
115-
# replace_audio_in_video(video, audio_path, f'final_output/{original_audio_name}-{target_lang_code}.mp4')
120+
# replace_audio_in_video(video, audio_path, f'final_output/{original_audio_name}-{target_lang_code}.mp4')
121+
122+
# Close log output file
123+
# sys.stdout = sys.__stdout__
124+
# log_output_file.close()

tools/video_editing.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,13 @@ def adjust_video_speed_and_replace_audio(video_path, background_audio_path, star
230230
'-y',
231231
'-i', adjusted_video,
232232
'-i', synthesized_audio_path,
233-
'-c:v', 'copy',
233+
'-c:v', 'libx264',
234234
'-c:a', 'aac',
235+
'-b:a', '192k',
235236
'-shortest',
236237
output_path
237238
]
239+
238240
print("Combining adjusted video and synthesized audio...")
239241
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
240242
if result.returncode != 0:
@@ -327,22 +329,26 @@ def extract_video_segment(video_path, background_audio_path, start_time, duratio
327329

328330
def create_concat_file(segment_video_paths, concat_file_path):
329331
"""
330-
Create a text file listing the video segments for FFmpeg concatenation.
332+
Create a text file listing the video segments for FFmpeg concatenation,
333+
including only files larger than a specified size.
331334
332335
Parameters
333336
----------
334337
segment_video_paths : list of str
335338
The list of paths to the video segments.
336339
concat_file_path : str
337340
The path to save the concat file.
338-
339-
Returns
340-
-------
341-
None
342341
"""
342+
min_file_size_bytes = 300 # Minimum file size in bytes
343+
343344
with open(concat_file_path, 'w') as f:
344345
for segment_path in segment_video_paths:
345-
f.write(f"file '{os.path.abspath(segment_path)}'\n")
346+
# Check if the file exists and is larger than the minimum size
347+
if os.path.isfile(segment_path) and os.path.getsize(segment_path) > min_file_size_bytes:
348+
f.write(f"file '{os.path.abspath(segment_path)}'\n")
349+
else:
350+
print(f"Excluding segment {segment_path} due to insufficient file size.")
351+
346352

347353
def concatenate_segments(concat_file_path, output_video_path):
348354
"""
@@ -367,7 +373,10 @@ def concatenate_segments(concat_file_path, output_video_path):
367373
'-f', 'concat',
368374
'-safe', '0',
369375
'-i', concat_file_path,
370-
'-c', 'copy',
376+
'-c:v', 'libx264',
377+
'-c:a', 'aac',
378+
'-strict', 'experimental',
379+
'-b:a', '192k',
371380
output_video_path
372381
]
373382
# subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

0 commit comments

Comments
 (0)