AKASH V
AKASH V

Reputation: 25

How to play video with the audio in open cv python from the particular frame?

I have to play a video from a particular frame, which i'm able to do with opencv, but how to play an audio of that particular video I have the frame from where i have to start playing my video, i just want the corresponding audio too.

cap = cv2.VideoCapture('tcs.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
count = 0
success = True
while success:
    success,frame = cap.read()
    count+=1
    ts = count/fps
    if t == ts:
      print("time stamp of current frame:",count/fps)
      print("Press Q to quit")
      f_count = count
cap.set(cv2.CAP_PROP_POS_FRAMES, f_count)
# Check if camera opened successfully 
if (cap.isOpened()== False):  
  print("Error opening video  file") 

# Read until video is completed 
while(cap.isOpened()): 

  # Capture frame-by-frame 
  ret, frame = cap.read() 
  if ret == True: 

    # Display the resulting frame 
    cv2.imshow('Frame', frame) 

    # Press Q on keyboard to  exit 
    if cv2.waitKey(25) & 0xFF == ord('q'): 
      break

  # Break the loop 
  else:  
    break

# When everything done, release  
# the video capture object 
cap.release() 

# Closes all the frames 
cv2.destroyAllWindows() 

Upvotes: 2

Views: 2185

Answers (1)

Rakeeb Hossain
Rakeeb Hossain

Reputation: 363

OpenCV is a library for computer vision. To play audio, you will have to use another library (i.e. in C++, one would use FFMPEG).

An alternative in Python is ffpyplayer (https://pypi.org/project/ffpyplayer/), which is based on FFMPEG.

You can use OpenCV to process the frame, and display the frame while concurrently playing the audio frame grabbed by ffpyplayer.

A better option would be to process and write the video file to disk in OpenCV, then play it with python-VLC.

Upvotes: 4

Related Questions