1. Introduction to main functions
- Video brightness adjustment, you can play brighter or darker video
- Video playback speed adjustment
- Adjust the speed and brightness of video playback through the keyboard
2. Brief ideas
For brightness adjustment, we should first do a good job of different brightness of the video. During the process of adjusting the brightness, we can jump between different brightness videos at the same frame to achieve continuous playback of different brightness. The video can be played at the specified frame position. (I feel unreasonable and mentally disabled, right? Let's go first. There's still no problem with the small video. I'm sure I can't do this for the film. It's just a practice, at least an idea)
Adjust the playback speed by accelerating or decreasing the time between frames.
3. Function realization
(some libraries need to be installed. We won't introduce them in detail here. Baidu is sure that everyone can complete them)
Let's explain the code paragraph by paragraph.
3.1} get videos with different brightness
First, there is a video to play. Then you can get videos with different brightness directly through the code. The code is also very simple. You don't need to fight with the VIP of some video converters.
#Install movie library pip install moviepy
from moviepy.editor import* clip = VideoFileClip(r"D:/python_test/opao_timeX1.0.mp4") clipColorx = clip.fx(vfx.colorx,0.1) #The n-fold new video of the imported video brightness is obtained by adjusting the number clipColorx.write_videofile (r"D:/python_test/opao_timeX0.1.mp4")
result:
3.2 installation of CV Library
In the command box of cmd, enter:
pip install opencv-python
3.3 video playback
The notes are very detailed and will not be repeated too much.
This code allows the video to play normally. Being able to play is the basis of later adjustment.
#——————————————————————————————Play video—————————————————————— #————————Add your own video playback path -—————————— video_path="D:/python_test/opao_timeX0.1.mp4" # Create a video reading and writing class video_capture=cv2.VideoCapture(video_path) #fps size of read video, number of frames transmitted per second 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)) #Read video duration (total frames) total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) #print("[INFO] {} total frames in video".format(total)) #Sets the frame from which the video is read frameToStart = 00 video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart); #Show video current_frame=frameToStart while True: success, frame = video_capture.read() time.sleep(0.0267) # cv2.waitKey(40) if success == False: break #Set the delay to reduce the playback speed # time.sleep(0.0188) #Custom image size h, w = frame.shape[:2] # Three channels size = (int(w * 1.0), int(h * 1.0)) frame = cv2.resize(frame, size) #Displays the played time and total time of the current video #Calculate current time 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) # {< parameter serial number >: < fill > < align) > < width > <, > < precision > < type >} Time_now_vs_total="Time:{:>3}:{:>02}|{:>3}:{:0>2}".format(now_minutes,now_seconds,total_minutes,total_second) print(Time_now_vs_total)
3.4 the keyboard controls the fast playback, slow playback, pause and exit of video
#--------The keyboard controls the fast playback, slow playback, pause and exit of video--------------- #Read keyboard value key = cv2.waitKey(1) & 0xff #Press z to increase the delay and j to decrease the delay. "zeng" and "jian" are implemented when the value is increased to a value 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) #Set pause when space is pressed if key == ord(" "): cv2.waitKey(1) #Set Q to exit when pressed if key == ord("q"): break
3.5 video brightness adjustment
I use the key u to indicate "up", that is, increase the brightness, "d" means "down", to reduce the brightness. Because the previous file names are 0.1, 0.4, 0.7, 1.0, 1.3, etc., the brightness is increased by 0.3 or decreased by 0.3 every time. When brightness is greater than 2 (at most 1.9 times brightness) or less than 0 (at least 0.1 times brightness), count_keyd or count_keyu is zeroed and brightness is set to the threshold.
Special reminder: when changing files under different paths, you should modify the video_ Number of path [: 25]
new_video_path = video_path[:25]+brightness_str+'.mp4'
#Press U, and the UP counter increases by 1, indicating that the brightness increases, i.e. 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 #Ensure that the result of brightness is to retain one decimal place, and there is often an error of 0.00000000000000009 or 0.00000000000000001 during calculation 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) #Press D and the down counter increases by 1, indicating that the brightness is down, i.e. down if key == ord("d"): count_keyd += 1 brightness = start_brightness + count_keyu * 0.3 - count_keyd * 0.3 #Variable representing brightness if(brightness<start_brightness): brightness = 0.1 count_keyd = 0 #Ensure that the result of brightness is to retain one decimal place, and there is often an error of 0.00000000000000009 or 0.00000000000000001 during calculation 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) #Manually count the number of video frames current_frame += 1
4. Complete code
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 #——————————————————————————————Play video—————————————————————— #————————Add your own video playback path -—————————— video_path="D:/python_test/opao_timeX0.1.mp4" # Create a video reading and writing class video_capture=cv2.VideoCapture(video_path) #fps size of read video, number of frames transmitted per second 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)) #Read video duration (total frames) total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) #print("[INFO] {} total frames in video".format(total)) #Sets the frame from which the video is read #From : https://blog.csdn.net/luqinwei/article/details/87973472 frameToStart = 00 video_capture.set(cv2.CAP_PROP_POS_FRAMES, frameToStart); #Show video current_frame=frameToStart while True: success, frame = video_capture.read() time.sleep(0.0267) # cv2.waitKey(40) if success == False: break #Set the delay to reduce the playback speed # time.sleep(0.0188) #Custom image size h, w = frame.shape[:2] # Three channels size = (int(w * 1.0), int(h * 1.0)) frame = cv2.resize(frame, size) #Displays the played time and total time of the current video #Calculate current time 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) # {< parameter serial number >: < fill > < align) > < width > <, > < precision > < type >} Time_now_vs_total="Time:{:>3}:{:>02}|{:>3}:{:0>2}".format(now_minutes,now_seconds,total_minutes,total_second) print(Time_now_vs_total) #--------Keyboard control video--------------- #Read keyboard value key = cv2.waitKey(1) & 0xff #Press z to increase the delay and j to decrease the delay. "zeng" and "jian" are implemented when the value is increased to a value 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) #Set pause when space is pressed if key == ord(" "): cv2.waitKey(1) #Set Q to exit when pressed if key == ord("q"): break #Press U, and the UP counter increases by 1, indicating that the brightness increases, i.e. 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 #Ensure that the result of brightness is to retain one decimal place, and there is often an error of 0.00000000000000009 or 0.00000000000000001 during calculation 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) #Press D and the down counter increases by 1, indicating that the brightness is down, i.e. down if key == ord("d"): count_keyd += 1 brightness = start_brightness + count_keyu * 0.3 - count_keyd * 0.3 #Variable representing brightness if(brightness<start_brightness): brightness = 0.1 count_keyd = 0 #Ensure that the result of brightness is to retain one decimal place, and there is often an error of 0.00000000000000009 or 0.00000000000000001 during calculation 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) #Manually count the number of video frames current_frame += 1 video_capture.release()