基於python + opencv的影片播放調節功能

專注的阿熊發表於2021-07-30

import numpy as np

import cv2

import time

start_brightness = 0.1

count_keyu = 0

count_keyd = 0  

count_keyz = 0

count_keyj = 0

delay_time = 0

# ——————————————————————————————播放影片——————————————————————

# ————————新增自己的影片播放路徑———————————

video_path="D:/python_test/opao_timeX0.1.mp4"

# 建立一個影片讀寫類

video_capture=cv2.VideoCapture(video_path)

# 讀取影片的 fps,   大小   每秒傳輸幀數

fps=video_capture.get(cv2.CAP_PROP_FPS)

size=(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH),video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

#print("fps: {}\nsize: {}".format(fps,size))

# 讀取影片時長(幀總數)

total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))

#print("[INFO] {} total frames in video".format(total))

# 設定從影片的第幾幀開始讀取

#From :  https://blog.csdn.net/luqinwei/article/details/87973472

frameToStart = 00

video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);

# 顯示影片

current_frame=frameToStart

while  True:

     success, frame = video_capture.read()   

     time.sleep(0.0267)

    # cv2.waitKey(40)    

     if  success == False:

         break

# 設定延時,降低播放速度

#    time.sleep(0.0188)

# 自定義影像大小

     h, w = frame.shape[:2]  # 三通道

     size = (int(w * 1.0), int(h * 1.0))

     frame = cv2.resize(frame, size)

# 顯示當前影片已播放時間和總時間

# 計算當前時間

     now_seconds=int(current_frame /fps%60)

     now_minutes=int(current_frame/fps/60)

     total_second=int(total /fps%60)

     total_minutes=int(total/fps/60)

#   { < 引數序號 > : < 填充 > < 對齊) > < 寬度 > <,> <. 精度 > < 型別 >}.

Time_now_vs_total="Time:{:>3}:{:>02}|{:>3}:{:0>2}".format(now_minutes,now_seconds,total_minutes,total_second)

     print(Time_now_vs_total)

#-------- 鍵盤控制影片 ---------------

     # 讀取鍵盤值

     key = cv2.waitKey(1) & 0xff

     # 按鍵 z 增長延時,外匯跟單gendan5.com j 減少延時。“ zeng ”、“ jian , 實現在增長到值開始建

     if key == ord("z"):

         count_keyz += 1

         delay_time = 0 + 0.02 * count_keyz

         if(delay_time>1.0):

             delay_time = 1.0

     if key == ord("j"):

             count_keyj += 1

             delay_time = delay_time - 0.02 * count_keyj

             if(delay_time<0):

                 delay_time = 0

             time.sleep(delay_time)

#    print(delay_time)

     time.sleep(delay_time)

     # 設定空格按下時暫停

     if key == ord(" "):

         cv2.waitKey(1)

     # 設定 Q 按下時退出

     if key == ord("q"):

         break

     # 按下 U ,上升計數器加 1 ,表示亮度增加,即 UP

     if key == ord("u"):

         count_keyu +=1

      #   print(count_keyu)

         brightness  = start_brightness + count_keyu * 0.3

       #  print(brightness)

         if(brightness>2):

             brightness = 1.9

             count_keyu = 0            

         # 確保 brightness 的結果是保留一位小數,計算時經常出現 0.00000000000000009 0.00000000000000001 的誤差

         brightness_str =format(brightness, '.1f')            

         new_video_path = video_path[:25]+brightness_str+'.mp4'

         video_capture = cv2.VideoCapture(new_video_path)

         frameToStart = now_seconds * fps

         video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);

         print(new_video_path)

     # 按下 D ,下降計數器加 1 ,表示亮度下調,即 down

     if key == ord("d"):

          count_keyd += 1

          brightness = start_brightness + count_keyu * 0.3 - count_keyd * 0.3  # 表示亮度的變數

          if(brightness<start_brightness):

              brightness = 0.1

              count_keyd = 0       

          # 確保 brightness 的結果是保留一位小數,計算時經常出現 0.00000000000000009 0.00000000000000001 的誤差

          brightness_str = format(brightness, '.1f')  

          new_video_path = video_path[:25]+brightness_str+'.mp4'

          video_capture=cv2.VideoCapture(new_video_path)

          frameToStart = now_seconds * fps

          video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);

#         print(new_video_path)

     #  putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None):

     cv2.putText(frame,Time_now_vs_total,(300,50),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),3)

     cv2.imshow("frame",frame)

     # 人工對影片幀數進行計數

     current_frame += 1

video_capture.release()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2784395/,如需轉載,請註明出處,否則將追究法律責任。

相關文章