Skip to content

Instantly share code, notes, and snippets.

@omair18
Created September 19, 2019 07:40
Show Gist options
  • Select an option

  • Save omair18/a408a7088b1043a356e5315bccaa9aa0 to your computer and use it in GitHub Desktop.

Select an option

Save omair18/a408a7088b1043a356e5315bccaa9aa0 to your computer and use it in GitHub Desktop.

Revisions

  1. omair18 created this gist Sep 19, 2019.
    79 changes: 79 additions & 0 deletions test_vid_write.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    import numpy as np
    import cv2
    from multiprocessing import Process

    def send():
    #originals
    #cap_send = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER) #orginal
    #out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 20, (320,240), True) #original
    #cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    ######

    #cap_send = cv2.VideoCapture('filesrc location=/home/gpu02/Videos/Jeddah_DSC_4208.MOV ! decodebin ! videoconvert ! video/x-raw,format=RGB ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    #cap_send = cv2.VideoCapture('filesrc location=/home/gpu02/Videos/Jeddah_DSC_4208.MOV ! decodebin name=dec ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    #cap_send = cv2.VideoCapture('ximagesrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    #out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc ! video/x-h264 ! mpegtsmux name=mux ! queue ! udpsink host=127.0.0.1 port=5000 sync=true',cv2.CAP_GSTREAMER,0, 20, (320,240), True)
    cap_send = cv2.VideoCapture('filesrc location=/app/Jeddah_DSC_4208.MOV ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc ! rtph264pay ! udpsink host=127.0.0.1 port=5005 sync=true',cv2.CAP_GSTREAMER,0, 20, (1920,1080), True)


    if not cap_send.isOpened() or not out_send.isOpened():
    print('VideoCapture or VideoWriter not opened')
    exit(0)
    frame_num = 0
    while True:
    ret,frame = cap_send.read()

    if not ret:
    print('empty frame')
    break

    out_send.write(frame)
    print("**** Sent frame #: {} ****".format(frame_num))
    frame_num+=1
    #cv2.imshow('send', frame)
    #if cv2.waitKey(1)&0xFF == ord('q'):
    # break

    cap_send.release()
    out_send.release()

    def receive():
    print("Recieve Starting")
    #cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    cap_receive = cv2.VideoCapture('udpsrc port=5005 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
    #cap_receive = cv2.VideoCapture('')

    frame_width = int(cap_receive.get(3))
    frame_height = int(cap_receive.get(4))
    out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (1920,1080))

    if not cap_receive.isOpened():
    print('VideoCapture not opened')
    exit(0)
    frame_num = 0
    while True:
    ret,frame = cap_receive.read()

    if not ret:
    print('empty frame')
    break
    print("----Received frame #: {} ----".format(frame_num))
    out.write(frame)
    #cv2.imshow('receive', frame)
    frame_num+=1
    if cv2.waitKey(1)&0xFF == ord('q'):
    break

    cap_receive.release()
    out.release()

    if __name__ == '__main__':
    s = Process(target=send)
    r = Process(target=receive)
    s.start()
    r.start()
    s.join()
    r.join()

    #cv2.destroyAllWindows()