linux ubuntu 20.04 command #52

Open
opened 2 years ago by rbsetiawan · 0 comments
Owner

main.py

# stream only

import subprocess
import cv2
import time
rtmp_url = "rtmp://gisportal.id:1935/hls/rpi2wcam"

# In my mac webcamera is 0, also you can set a video file name instead, for example "/home/user/demo.mp4"
path = 0
cap = cv2.VideoCapture(path)

# gather video info to ffmpeg
fps = int(cap.get(cv2.CAP_PROP_FPS))
# fps = 12
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# widths = width*2
# heights = widths*2
print(fps, width, height)


# command and params for ffmpeg
command = ['ffmpeg',
           '-y',
           '-f', 'rawvideo',
           '-vcodec', 'rawvideo',
           '-pix_fmt', 'bgr24',
           '-s', "{}x{}".format(width, height),
           '-r', str(fps),
           '-i', '-',
           '-c:v', 'libx264',
           '-pix_fmt', 'yuv420p',
           '-preset', 'ultrafast',
           '-f', 'flv',
           rtmp_url]

# using subprocess and pipe to fetch frame data
p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=False)




while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("frame read failed")
        break

    # YOUR CODE FOR PROCESSING FRAME HERE
    # write to pipe
    p.stdin.write(frame.tobytes())
  • ubuntu version
lsb_release -a

image

Install ssh
  • install ssh
sudo apt update
sudo apt install openssh-server
sudo systemctl status ssh
  • allow ssh port 22
sudo ufw allow ssh
  • auto start ssh
sudo systemctl enable --now ssh
  • list usb device
lsusb

image

Python install
  • cek python3 version
python3 --version
pip3 --version

image

  • install pip3
sudo apt install python3-pip
pip3 --version

image

USB CAM install
  • ffmpeg
sudo apt-get install v4l-utils ffmpeg
  • cek usb device cam
v4l2-ctl --list-devices

image

  • restart host
sudo reboot
  • cek usb device cam
v4l2-ctl --list-devices

image

  • luvcview camera configurable
sudo apt-get install luvcview

image

sudo apt-get install guvcview

image

sudo apt-get install fswebcam
  • opencv
pip3 install opencv-python

image

  • runing webcamp stream on background from ssh
    & tanda tersebut untuk lock command tetap berjalan saat ssh di close
nohup python3 streamcam.py &

image

refrensi

  • linuxize : How to Enable SSH on Ubuntu 20.04
  • rosehosting : How to Install pip on Ubuntu 20.04
  • substack : How To Use Your Webcam On Linux
  • youtube : Install a USB camera or a webcam on Ubuntu - Linux
main.py ``` # stream only import subprocess import cv2 import time rtmp_url = "rtmp://gisportal.id:1935/hls/rpi2wcam" # In my mac webcamera is 0, also you can set a video file name instead, for example "/home/user/demo.mp4" path = 0 cap = cv2.VideoCapture(path) # gather video info to ffmpeg fps = int(cap.get(cv2.CAP_PROP_FPS)) # fps = 12 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # widths = width*2 # heights = widths*2 print(fps, width, height) # command and params for ffmpeg command = ['ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-pix_fmt', 'bgr24', '-s', "{}x{}".format(width, height), '-r', str(fps), '-i', '-', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'ultrafast', '-f', 'flv', rtmp_url] # using subprocess and pipe to fetch frame data p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=False) while cap.isOpened(): ret, frame = cap.read() if not ret: print("frame read failed") break # YOUR CODE FOR PROCESSING FRAME HERE # write to pipe p.stdin.write(frame.tobytes()) ``` - ubuntu version ``` lsb_release -a ``` ![image](/attachments/2962331c-1d71-4c14-835a-7012d0d0f332) <details> <summary>Install ssh</summary> - install ssh ``` sudo apt update sudo apt install openssh-server sudo systemctl status ssh ``` - allow ssh port 22 ``` sudo ufw allow ssh ``` - auto start ssh ``` sudo systemctl enable --now ssh ``` </details> - list usb device ``` lsusb ``` ![image](/attachments/22e9c556-320a-4f0e-a61d-3edaabc686db) <details> <summary>Python install</summary> - cek python3 version ``` python3 --version pip3 --version ``` ![image](/attachments/251ade5f-6180-4042-9471-d032bdda09d4) - install pip3 ``` sudo apt install python3-pip pip3 --version ``` ![image](/attachments/e4c68fb0-eb41-4783-aefd-65194ff452b6) </details> <details> <summary>USB CAM install</summary> - ffmpeg ``` sudo apt-get install v4l-utils ffmpeg ``` - cek usb device cam ``` v4l2-ctl --list-devices ``` ![image](/attachments/65a88c2e-f848-499c-b60b-93ffb7cdaf5a) - restart host ``` sudo reboot ``` - cek usb device cam ``` v4l2-ctl --list-devices ``` ![image](/attachments/892aec85-572a-4cbf-b5d0-98db3123c516) - luvcview camera configurable ``` sudo apt-get install luvcview ``` ![image](/attachments/969dc2da-3671-4dfe-be01-bfbb5b4632e5) ``` sudo apt-get install guvcview ``` ![image](/attachments/168f83cd-9c1a-4935-8214-31bf619fe512) ``` sudo apt-get install fswebcam ``` </details> - opencv ``` pip3 install opencv-python ``` ![image](/attachments/e791fb50-c3ac-47a0-a1f6-0b5964e6e235) - runing webcamp stream on background from ssh `&` tanda tersebut untuk lock command tetap berjalan saat ssh di close ``` nohup python3 streamcam.py & ``` ![image](/attachments/ebb52910-b219-4cc6-b16c-6fa76b67d768) # refrensi - [linuxize](https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-20-04/) : How to Enable SSH on Ubuntu 20.04 - [rosehosting](https://www.rosehosting.com/blog/how-to-install-pip-on-ubuntu-20-04/) : How to Install pip on Ubuntu 20.04 - [substack](https://glovesoff.substack.com/p/how-to-use-your-webcam-on-linux) : How To Use Your Webcam On Linux - [youtube](https://www.youtube.com/watch?v=1e7dbiRdtWk) : Install a USB camera or a webcam on Ubuntu - Linux
rbsetiawan added the
Linux
label 2 years ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.