new scripts

main
oscar 1 month ago
parent 0d30a4de1c
commit f726684c1d

@ -0,0 +1,100 @@
from moviepy.editor import VideoFileClip, concatenate_videoclips
import os, cv2
def add_intro_to_video(input_video, intro_video='intro.mp4', output_video='output.mp4'):
clip_main = VideoFileClip(input_video)
clip_intro = VideoFileClip(intro_video).resize(clip_main.size).set_fps(clip_main.fps)
if clip_main.audio is not None and clip_intro.audio is None:
from moviepy.editor import AudioArrayClip
silent_audio = AudioArrayClip([[0] * int(clip_intro.duration * clip_main.audio.fps)], fps=clip_main.audio.fps)
clip_intro = clip_intro.set_audio(silent_audio)
final_clip = concatenate_videoclips([clip_intro, clip_main])
final_clip.write_videofile(output_video, codec='libx264')
def get_duration(input_file):
if not os.path.isfile(input_file):
print('Input file does not exist')
return 0
try:
video = cv2.VideoCapture(input_file)
frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
fps = video.get(cv2.CAP_PROP_FPS)
duration = frames / fps
video.release()
return int(duration)
except Exception as e:
print(e)
return 0
def generate_thumbnails(input_file, filename):
output_folder = 'temp/'
if not os.path.isfile(input_file):
raise ValueError('Input file does not exist')
if not os.path.exists(output_folder):
os.makedirs(output_folder)
posterPath = os.path.join(output_folder, f'{filename}.jpg')
previewPath = os.path.join(output_folder, f'{filename}.mp4')
clip = VideoFileClip(input_file)
duration = clip.duration
interval = duration / 11.0
start_time_first_clip = 0 * interval
try:
clip.save_frame(posterPath, t=start_time_first_clip)
except:
pass
clips = []
for i in range(10):
start_time = i * interval
end_time = start_time + 1
clips.append(clip.subclip(start_time, end_time))
final_clip = concatenate_videoclips(clips).resize(newsize=(384, 216)).without_audio()
final_clip.write_videofile(previewPath, fps=24, codec="libx264")
for subclip in clips:
subclip.close()
clip.close()
final_clip.close()
return posterPath, previewPath
def split_video(file_path, segment_size_gb=8):
import subprocess
# Convert GB to bytes
segment_size_bytes = segment_size_gb * 1024 * 1024 * 1024
# Get the total size of the video file
total_size_bytes = os.path.getsize(file_path)
# Calculate the number of segments needed
num_segments = total_size_bytes // segment_size_bytes + 1
# Get the duration of the video file
duration = get_duration(file_path)
# Calculate the duration of each segment
segment_duration = duration / num_segments
# Generate output file pattern
file_name, file_extension = os.path.splitext(file_path)
output_pattern = f"{file_name}_segment_%03d{file_extension}"
# Run FFmpeg command to split the video
command = [
"ffmpeg", "-i", file_path, "-c", "copy", "-map", "0",
"-segment_time", str(segment_duration), "-f", "segment", output_pattern
]
subprocess.run(command)

@ -0,0 +1,49 @@
import os, shutil, config
from tqdm import tqdm
if __name__ == "__main__":
output_dir = 'U:/streamaster/streams/'
conn, cursor = config.get_local_db_connection()
cursor.execute("SELECT * FROM videos WHERE status != 'missing' AND filepath NOT LIKE %s ORDER BY size ASC;", ("%" + output_dir + "%",))
videos = cursor.fetchall()
# process the videos
output_dir = "U:/streamaster/streams/"
os.makedirs(output_dir, exist_ok=True)
total_size = int(sum([video['size'] for video in videos]))
total_moved = 0
with tqdm(total=total_size, desc=f"Moved [{total_moved}/{len(videos)}] videos", unit="MB") as pbar:
for video in videos:
pbar.update(int(video["size"]))
username = video["username"]
video_path = video["filepath"]
if not video_path:
continue
user_folder = os.path.join(output_dir, username)
video_name = os.path.basename(video_path)
new_video_path = os.path.join(user_folder, video_name)
if os.path.exists(new_video_path):
cursor.execute("UPDATE videos SET filepath = %s WHERE id = %s;", (new_video_path, video["id"],))
conn.commit()
continue
if not os.path.exists(video_path):
continue
os.makedirs(user_folder, exist_ok=True)
# move the file to the new location
shutil.move(video_path, new_video_path)
cursor.execute("UPDATE videos SET filepath = %s WHERE id = %s;", (new_video_path, video["id"],))
conn.commit()
total_moved += 1
pbar.desc = f"Moved [{total_moved}/{len(videos)}] videos"
Loading…
Cancel
Save