1

I am working on a video editing project which edits the original video and creates a new file I got stuck becuse there is no sound in the new file which the software creates. So I want to copy the sound from the original file and paste it into the new file (I have video in the new file, but the video file doesn't have any sound - I want to get sound from the original file and use this sound in my new file)

Any ideas?

Here is my video editing script:

import io, re, requests
from PIL import  Image, ImageOps, ImageEnhance, ImageChops
import cv2
import numpy as np

cap = cv2.VideoCapture('original.mp4')

fps = cap.get(cv2.CAP_PROP_FPS)

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
   
size = (frame_width, frame_height)
result = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps,size)

while cap.isOpened():
    ret,frame = cap.read()
    
   #editing part

    result.write(edited_frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
gasper101
  • 102
  • 1
  • 12

1 Answers1

1

It seems you can extract the sound from the video file and then "pasting" it into the silent mp4 file using moviepy (didn't test this, based only on reading)

video = mp.VideoFileClip(r"sample.mp4")
video.audio.write_audiofile(r"extracted_sound.mp3")

and then paste the sound:

import moviepy.editor as mpe
my_clip = mpe.VideoFileClip('output_video.mp4')
audio_background = mpe.AudioFileClip('extracted_sound.mp3')
final_audio = mpe.CompositeAudioClip([my_clip.audio, audio_background])
final_clip = my_clip.set_audio(final_audio)

====

based on:

https://dev.to/itsaditya/extracting-audio-from-video-clips-using-python-9d8

Video editing with python : adding a background music to a video with sound