0

So i have a video with a duration of 15 seconds and at a specific time i need to insert a text field. Until now my code just reads a video and displays it. After that we extract frames and calculate the duration of each frame.

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('my_baby_dog.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:
        fps = cap.get(cv2.CAP_PROP_FPS)  # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        duration = frame_count / fps

        print('fps = ' + str(fps))
        print('number of frames = ' + str(frame_count))
        print('duration (S) = ' + str(duration))
        minutes = int(duration / 60)
        seconds = duration % 60
        print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))
        # Display the resulting frame
        frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        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()
medinaz
  • 23
  • 2
  • 12
  • Welcome to SO. Obviously, you have a programming problem, so your post is appropriate here, However, you didn't actually ask a question, and I personally couldn't determine where exactly your problem is. Could you edit your question? – Binarus Nov 23 '19 at 12:19
  • Since my video is 15 seconds i want to insert a text when time of video reaches 6 seconds (set a text into a specific time) – medinaz Nov 23 '19 at 12:51

1 Answers1

0

It sounds like you want to add a text overlay after 6 seconds. Assuming this overlay will continue until the video is finished, you will want to add an if statement to compare the duration to the start time of your text overlay. then display it.

import cv2
import numpy as np

start_time = 6

# 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('my_baby_dog.mp4')

........
while (cap.isOpened()):
    ........
        # ADD OVERLAY TEXT
        if start_time < duration:
            cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        cv2.imshow('Frame', frame)

Likewise you could start and stop the text overlay like this

import cv2
import numpy as np

start_time = 6
stop_time = 10
    ........
        # ADD OVERLAY TEXT
        if start_time < duration < stop_time:
            cv2.putText(img=frame, text='EKO', org=(int(frameWidth / 2 - 20), int(frameHeight / 2)),
                    fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=3,
                    color=(0, 255, 0))
        .......
ron
  • 186
  • 7
  • As long the interval [start_time,stop_time] is always true as long as these values are inside the range of whole duration of the video so the text will always pop up on the whole video duration. I need a function that can seek in the video whenever i set a specific time. Thanks for your answer :) – medinaz Nov 23 '19 at 14:11
  • This post will show you how to seek in a video. [Seeking using openCV](https://stackoverflow.com/questions/2974625/opencv-seek-function-rewind) – ron Nov 26 '19 at 21:25