I'm playing a mp4 file in python, but the sound of the video doesn't comes out, I searched for a while if it's anyway to play the sound, but I could not find anything. Does anyone knows how to play the sound?
I post the code that displays de video :
import cv2
import numpy as np
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('video.mp4')
# Check if camera opened successfully
#if (cap.isOpened()== False):
# print("Error opening video stream or 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()
It seems useless that OpenCV doesn't allow to play the sound. I'm using Python 3 by the way. Thank you.
ANSWER: OpenCV is a computer-vision library. It does not support audio. If you want to play sound, you can try ffpyplayer. – Thanks to >>> yushulx
You can use Pyglet to play video along with its audio....
Hope this helps...
import pyglet
vid_path='vid1.mp4' # Name of the video
window=pyglet.window.Window()
player = pyglet.media.Player()
source = pyglet.media.StreamingSource()
MediaLoad = pyglet.media.load(vid_path)
player.queue(MediaLoad)
player.play()
#window.event
def on_draw():
if player.source and player.source.video_format:
player.get_texture().blit(50,50)
pyglet.app.run()
Related
I'm trying to run the most simple script for viewing the laptop's camera in real time. But unfortunately after starting the window shows, but I get only a single frame displayed that doesn't ever get updated.
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('test', img)
if cv2.waitKey(-1):
break
cv2.destroyAllWindows()
cap.release()
I was following tutorials for installing this on Windows and installed it on separate environment, using pip and downloaded wheel. The window shows OK, and the image from camera is displayed but static. The program isn't hanged however, because it awaits a key being pressed and closes correctly afterwards.
What can be the cause?
Try using this loop:
while True:
ret, img = cap.read()
cv2.imshow('test', img)
keypressed = cv2.waitKey(30)
if keypressed == ord('q'):
break
The argument of cv2.waitKey(delay) is the delay in millisecond and the returned value is the key pressed:
The function waitKey waits for a key event infinitely (when 𝚍𝚎𝚕𝚊𝚢≤0 ) or for delay milliseconds, when it is positive. [..]
It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
See: https://docs.opencv.org/4.1.0/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7
Try this:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I hope this will work it is fast and easy solution.
you can capture image by pressing c and q for quit the window
import cv2
cap = cv2.VideoCapture(0)
count=0
while(True):
ret, frame = cap.read()
cv2.imshow("imshow",frame)
key=cv2.waitKey(30)
if key==ord('q'):
break
if key==ord('c'):
count+=1
cv2.imwrite('/home/user/Desktop/image'+str(count)+'.png', frame)
cap.release()
cv2.destroyAllWindows()
I have this code that plays a video, and detects something on it. Whenever it detects something in the video, I want to hear something, here is the code:
import cv2
import os
video_capture = cv2.VideoCapture('video')
while True:
_, frame = video_capture.read()
found = detect_something(frame)
if found :
os.system("aplay 'alarm'")
cv2.imshow('Video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
The problem is that, whenever it plays the alarm, the video freezes. I want the alarm to be played as a background sound. How can I do that?
What it needs is a tread:
import cv2
import os
from threading import Thread # Import Thread here
video_capture = cv2.VideoCapture('video')
def music(): # Define a function to go in the Thread
os.system("aplay 'alarm'")
while True:
_, frame = video_capture.read()
found = detect_something(frame)
if found :
mus = Thread(target=music) # Create a Thread each time found
mus.start() # Start the Thread as soon as created
cv2.imshow('Video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
Right now I am using a web cam and it works perfectly fine with the code below -:
capture = cv2.VideoCapture(0)
Now instead of web cam, I want to use the ip camera(https://192.168.0.60)
What would be the easiest way to do it with OpenCV(Python)?
I saw a bunch of posts, but couldn't find a straight answer to this.
Can someone help, who already got it running?
Thank you!
Firstly, you must find the exact url for your video stream, and that's best done with a web browser. For example I use IP Webcam app on android (com.pass.webcam) and it's stream will be on:
http://phone-ip-address:port/video
If I visit that url with a web browser, I can see the live stream. Make sure, that what you see is only the video stream, not a html page with the stream. If there is a html page, you can right-click and select Open image in new tab (in Chrome) to get to the stream.
However it looks like OpenCV can only read the video stream if the filename/url has the right suffix. Adding ?type=some.mjpeg worked for me. So the url would be:
http://phone-ip-address:port/video?type=some.mjpeg
Try visiting such an url in the web browser before you go for opencv.
Take a look at this example with python, OpenCV, IPCAM and hikvision
import numpy as np
import cv2
cap = cv2.VideoCapture()
cap.open("rtsp://USER:PASS#IP:PORT/Streaming/Channels/2")
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('Salida',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Image:
Get video from IPCAM with python and OpenCV
Here you go,
import numpy as np
import cv2
cap = cv2.VideoCapture('rtsp://<username_of_camera>:<password_of_camera#<ip_address_of_camera')
while(True):
ret, frame = cap.read()
cv2.imshow('Stream IP Camera OpenCV',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
For example:
import numpy as np
import cv2
cap = cv2.VideoCapture('rtsp://admin:admin#192.168.0.60')
while(True):
ret, frame = cap.read()
cv2.imshow('Stream IP camera opencv',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Then save the file as camera.py (.py), go to command prompt or terminal, locate file and type python camera.py or python <file_name>.py enter to run script.
If you want to exit from script windows just press "q" or close cmd.
Hope this helpful.
Here is an example for a Vivotek IP8136W IP Camera. It does not support streaming.
This code continuously grabs still frames, at about 2fps. No observed CPU loading.
import numpy as np
import cv2
# for webcams, request stream only once.
#cap = cv2.VideoCapture(0)
while(True):
# For single image IP cams, request frame every time.
cap = cv2.VideoCapture("http://root:0002A78D65F2#192.168.1.38/cgi-bin/viewer/video.jpg")
ret, frame = cap.read()
# Display the resulting frame
if ret:
cv2.imshow('camera',frame)
else:
print("error getting frame.")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Done. release the capture
cap.release()
cv2.destroyAllWindows()
The following code is trying to stream a video from an A.R Drone using python. When I run the code, I receive error error reading video feed. I am using OPENcv 3.3.0. Maybe I am suppose to download something. I am not entirely sure. I am a newbie to this.
import cv2
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
running = True
while running:
# get current frame of video
running, frame = cam.read()
if running:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
# escape key pressed
running = False
else:
# error reading frame
print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()
I'm working the FLIR thermal camera and trying to record its output but for some reason when I run my code I get an 8K file that does not contain anything. Also, nothing displays on my screen. I'm not sure what's missing, but can someone look at my code and let me know if I'm missing something? I'm new to Python so I might have missed something obvious:
import cv2
import time
if __name__ == "__main__":
# find the webcam
capture = cv2.VideoCapture("/dev/spidev0.0")
# video recorder
fourcc = cv2.cv.CV_FOURCC('I', '4', '2', '0') # cv2.VideoWriter_fourcc() does not exist
video_writer = cv2.VideoWriter("output.avi", fourcc, 8, (60, 80))
# record video
while (capture.isOpened()):
ret, frame = capture.read()
if ret:
resized_frame = cv2.resize(frame, (60, 80))
video_writer.write(resized_frame)
cv2.imshow('Video Stream', frame)
else:
break
capture.release()
video_writer.release()
cv2.destroyAllWindows()
I know that the camera works fine because I tested it with another program so I'm not sure why it's not working with this program.