{"id":222859,"date":"2021-07-30T10:55:30","date_gmt":"2021-07-30T02:55:30","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=222859"},"modified":"2021-07-21T08:57:34","modified_gmt":"2021-07-21T00:57:34","slug":"python-ev-ffmpeg","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-ev-ffmpeg.html","title":{"rendered":"\u7528 Python \u548c EV \u526a\u8f91\u77ed\u89c6\u9891\u6559\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u73b0\u5728\u5c0f\u89c6\u9891\u975e\u5e38\u6d41\u884c\uff0c\u5982\u4f55\u8fdb\u884c\u9ad8\u6548\u7b80\u4fbf\u7684\u89c6\u9891\u526a\u8f91\uff0c\u662f\u5404\u4e2aup\u4e3b\u7684\u5fc5\u5907\u6280\u80fd\u3002\u672c\u6587\u501f\u52a9python\u548cEV\u526a\u8f91\u5236\u4f5c\u6211\u4eec\u6240\u8981\u7684\u89c6\u9891\u6548\u679c\uff0c\u4e0d\u4ec5\u514d\u8d39\uff0c\u800c\u4e14\u6ca1\u6709\u5e7f\u544a\u6c34\u5370\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001\u5c06\u56fe\u7247\u8f6c\u5316\u6210\u89c6\u9891<\/strong><\/div>\n

\u901a\u8fc7python\u7684opencv\u5e93\u5c31\u53ef\u4ee5\u628a\u56fe\u7247\u8f6c\u6210\u89c6\u9891\uff0c\u8981\u6ce8\u610f\u7684\u662f\u628a\u5e27\u7387\u8bbe\u7f6e\u621030\uff0c\u65b9\u4fbf\u540e\u7eed\u548c\u5176\u4ed6\u89c6\u9891\u62fc\u63a5\uff0c\u56e0\u4e3a\u5e27\u7387\u4e0d\u540c\u4f1a\u5bfc\u81f4\u62fc\u63a5\u51fa\u9519\u3002<\/p>\n

\r\nimport cv2,os  \r\nimport matplotlib.pyplot as plt  \r\nfrom PIL import Image  \r\ncanshu=0.5  #\u8bbe\u7f6e\u89c6\u9891\u5206\u8fa8\u7387\u53c2\u6570  \r\nimg = Image.open('\u4e2d\u56fd\u7968\u636e\u53d1\u5c55\u62a5\u544a.jpg')  \r\nimg.save('ddd.jpg')  \r\nimg = cv2.imread('ddd.jpg') #\u8bbe\u7f6e\u4e00\u4e2a\u56fe\u7247\u7684\u5927\u5c0f\u4f5c\u4e3a\u6307\u5b9a\u5927\u5c0f  \r\nimgimgInfo = img.shape  \r\nsize = (int(imgInfo[1]*canshu),int(imgInfo[0]*canshu))  \r\nfourcc = cv2.VideoWriter_fourcc('M','J','P','G')   \r\nvideoWrite = cv2.VideoWriter('3.avi',fourcc,30,size,True)  #\u53c2\u6570\u4f9d\u6b21\u662f\uff1a\u6587\u4ef6\u8def\u5f84\uff1b\u6307\u5b9a\u7f16\u7801\u5668\uff1b\u5e27\u7387\uff1b\u753b\u9762\u5927\u5c0f\uff1b\u662f\u5f69\u8272\u8fd8\u662f\u9ed1\u8272  \r\nfile_list = os.listdir('.\/1\/\/')   #\u83b7\u53d6\u6587\u4ef6\u5939\u91cc\u7684\u5217\u8868  \r\nfor i in range(0,len(file_list)):  \r\n    fileName ='.\/1\/\/'+file_list[i] \r\n    img = Image.open(fileName)  \r\n    #\u6839\u636e\u6837\u672c\u56fe\u7247\u8c03\u6574\u56fe\u7247\u5927\u5c0f  \r\n    (x00,y00) = img.size  \r\n    x_s = x00*canshu  \r\n    y_s = int(y00 * x_s \/ x00)   \r\n    imgimg = img.resize(size,Image.ANTIALIAS)  \r\n    img.save('ddd.jpg')  \r\n    img = cv2.imread('ddd.jpg') #\u8bfb\u53d6\u56fe\u50cf  \r\n    #\u56e0\u4e3a\u662f\u5355\u5f20\u56fe\u7247\u7684\u65f6\u95f4\u592a\u77ed\uff0c\u6bcf\u4e00\u5f20\u56fe\u7247\u63d2\u516520\u5e27  \r\n    for j in range(0,20):  \r\n        videoWrite.write(img)  \r\nvideoWrite.release() \r\n<\/pre>\n

\u4f46\u662f\u5982\u679c\u8981\u5c06\u56fe\u7247\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u52a8\u753b\u6548\u679c\uff0c\u4f8b\u5982\u505a\u4e00\u4e2a\u753b\u9762\u9010\u6e10\u7f29\u5c0f\u7684\u52a8\u753b\uff0c\u90a3\u5c31\u8981\u501f\u52a9EV\u526a\u8f91\uff0c\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u52a0\u4e0a\u767d\u80cc\u666f\uff0c\u7136\u540e\u5bfc\u51fa\u89c6\u9891\u3002<\/p>\n

\"\"<\/p>\n

\u6ce8\u610f\u5bfc\u51fa\u7684\u65f6\u5019\u8981\u8bbe\u7f6e\u5e27\u7387\uff0c\u5982\u679c\u65f6\u95f4\u8fc7\u957f\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8bbe\u7f6e\u5e27\u7387\u4f4e\u4e00\u70b9\uff0c\u7136\u540e\u7528python\u7a0b\u5e8f\u628a\u64ad\u653e\u901f\u5ea6\u63d0\u5347\uff0c\u5e27\u7387\u4e5f\u4f1a\u76f8\u5e94\u63d0\u9ad8\u3002<\/p>\n

\r\nimport cv2  \r\nvdop = \"4.mp4\"#\u8f93\u5165\u89c6\u9891\u8def\u5f84  \r\ncap = cv2.VideoCapture(vdop)   \r\nfps = cap.get(cv2.CAP_PROP_FPS) #\u83b7\u53d6\u8f93\u5165\u89c6\u9891\u7684\u5e27\u7387 \r\nsize = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),  \r\n        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))#\u83b7\u53d6\u8f93\u5165\u89c6\u9891\u7684\u5927\u5c0f  \r\nfourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')  #These are the FOURCCs to compressed formats  \r\nout_path = \"8.mp4\" #\u8f93\u51fa2\u500d\u901f\u7684avi\u683c\u5f0f\u7684\u89c6\u9891\u8def\u5f84  \r\noutput_viedo = cv2.VideoWriter()  \r\nfps = (29.5\/26.8)*fps #2\u500d\u901f\u5904\u7406  \r\n#isColor\uff1a\u5982\u679c\u8be5\u4f4d\u503c\u4e3aTure\uff0c\u89e3\u7801\u5668\u4f1a\u8fdb\u884c\u989c\u8272\u6846\u67b6\u7684\u89e3\u7801\uff0c\u5426\u5219\u4f1a\u4f7f\u7528\u7070\u5ea6\u8fdb\u884c\u989c\u8272\u67b6\u6784  \r\noutput_viedo.open(out_path , fourcc, fps, size, isColor=True)  \r\nrval = True  \r\nwhile rval:  \r\n    rval, img = cap.read()#\u9010\u5e27\u8bfb\u53d6\u539f\u89c6\u9891  \r\n    output_viedo.write(img)#\u5199\u5165\u89c6\u9891\u5e27  \r\noutput_viedo.release()  \r\ncap.release() \r\n<\/pre>\n

\u7528opencv\u5904\u7406\u8fc7\u7684\u89c6\u9891\u4f1a\u5931\u53bb\u97f3\u9891\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528ffmpeg\u6765\u8c03\u901f\uff0c\u53ef\u4ee5\u4fdd\u7559\u58f0\u97f3\uff0c\u7279\u522b\u9002\u5408\u5fae\u8c03\u3002\u8981\u4f7f\u7528FFmpeg\uff0c\u5b89\u88c5\u5b8c\u4e4b\u540e\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u5c31\u80fd\u5728python\u8c03\u7528FFmpeg\u3002<\/p>\n

\r\nimport os  \r\nos.system('ffmpeg -i 4.mp4 -vf  \"setpts=2*PTS\" 8.mp4')  #\u56fe\u50cf\u4e24\u500d\u6162\u901f  \r\n<\/pre>\n
\u4e8c\u3001\u5c06\u89c6\u9891\u6dfb\u52a0\u97f3\u9891\u4f5c\u4e3a\u80cc\u666f\u97f3\u4e50 <\/strong><\/div>\n
\r\nimport subprocess,os  \r\nmp4_f = '.\/2.avi'  \r\nmp3_f = '.\/\u80cc\u666f\u97f3.mp3'  \r\nn_mp4_n = 'new' + mp4_f.split('\/')[-1] \r\nn_mp4_f = mp4_f.replace(mp4_f.split('\/')[-1],n_mp4_n)  \r\ncom = f'D:\\\\ffmpeg\\\\bin\\\\ffmpeg.exe -i \"{mp3_f}\" -i \"{mp4_f}\" ' \\ f' -acodec copy -vcodec copy \"{n_mp4_f}\"'  \r\nprint(com)  \r\nos.system(com) \r\n<\/pre>\n
\u4e09\u3001\u5408\u5e76\u89c6\u9891<\/strong><\/div>\n

1.\u526a\u5207\u89c6\u9891<\/p>\n

\r\nimport os  \r\nimport subprocess,cv2  \r\ncom = f'ffmpeg -i 5.mp4 -c copy -t 00:00:20.0 output.mp4'  # \u622a\u53d6\u524d20\u79d2  -ss 00:00:00.0   \u8d77\u59cb\u4f4d\u7f6e  \r\nos.system(com) \r\n<\/pre>\n

2.\u8981\u5c06\u4e0d\u540c\u7684\u89c6\u9891\u5408\u5e76\uff0c\u9996\u5148\u8981\u786e\u4fdd\u5e27\u7387\u548c\u753b\u5e03\u5927\u5c0f\u4e00\u6837\uff0c\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u5408\u5e76\u8d77\u6765\u4e0d\u51fa\u9519\u3002\u4e0b\u9762\u4ee3\u7801\u662f\u8c03\u6574\u753b\u5e03\u3002\u5c06\u5f62\u72b6\u4e0d\u540c\u7684\u89c6\u9891\u753b\u9762\uff0c\u8981\u6309\u7167\u539f\u89c6\u9891\u7684\u6bd4\u4f8b\uff0c\u8c03\u6574\u5230\u4e00\u4e2a\u6846\u91cc\u9762\u53bb\u3002<\/p>\n

\r\nimport subprocess,os  \r\nimport cv2  \r\n#\u83b7\u53d6\u6837\u672c\u7684\u753b\u5e03\u5927\u5c0f  \r\nvideo_path = \"2\\\\1.mp4\"  \r\ncap = cv2.VideoCapture(video_path)  \r\nframe_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))# \u83b7\u53d6\u89c6\u9891\u9ad8\u5ea6  \r\nframe_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# \u83b7\u53d6\u89c6\u9891\u5bbd\u5ea6  \r\nfps = cap.get(cv2.CAP_PROP_FPS) #\u89c6\u9891\u5e73\u5747\u5e27\u7387  \r\nh0=frame_height  \r\nw0=frame_width  \r\nfile_list = os.listdir('3\\\\')   #\u83b7\u53d6\u6587\u4ef6\u5939\u91cc\u7684\u6240\u6709\u6587\u4ef6\u5217\u8868  \r\nfor i in file_list:  \r\n  #\u83b7\u53d6\u5f53\u524d\u89c6\u9891\u7684  \r\n  video_path = '3\\\\'+i  \r\n  cap = cv2.VideoCapture(video_path)  \r\n  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) \r\n  # \u83b7\u53d6\u89c6\u9891\u9ad8\u5ea6\u3001\u5bbd\u5ea6  \r\n  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  \r\n  h=frame_height  \r\n  w=frame_width   \r\n  if h\/w>h0\/w0:    \r\n    strn=f'ffmpeg -i %s -vf \"scale=(%s*%s\/%s):%s,pad=%s:%s:(%s-(%s*%s\/%s))\/2:0:black\" %s'%  (video_path,w,h0,h,h0,        w0,h0,w0,w,h0,h,i)  \r\n  else:               \r\n     strn=f'ffmpeg -i  %s -vf \"scale=%s:%s*%s\/%s,pad=%s:%s:0:((%s-(%s*%s\/%s))\/2):black\"  %s'%    (video_path,w0,w0,h,w,       w0,h0,          h0,w0,h,w,i)  \r\n                              #\u672c\u56fe\u7f29\u653e\u540e=\u5bbd\uff1a\u9ad8\uff0c\u4f4d\u7f6e=\u603b\u5bbd\uff1a\u603b\u9ad8\uff1a\u6c34\u5e73\u65b9\u5411\u653e\u7f6e\u7684\u4f4d\u7f6e\uff1a\u5782\u76f4\u65b9\u5411\u653e\u7f6e\u7684\u4f4d\u7f6e  \r\n  os.system(strn) \r\n<\/pre>\n

3.\u5c06\u89c6\u9891\u5408\u5e76<\/p>\n

\u5c06\u89c6\u9891\u5148\u8f6c\u5316\u6210ts\u683c\u5f0f\uff0c\u7136\u540e\u518d\u5408\u5e76\uff0c\u6210\u529f\u7387\u66f4\u9ad8\u3002<\/p>\n

\r\nimport os  \r\nlista='kaishiwizhi'  \r\n#\u5148\u83b7\u53d6\u8fd9\u4e9bMP4\u6587\u4ef6\uff0c\u8f6c\u6362\u6210ts\u683c\u5f0f  \r\nfor each in os.listdir():  \r\n    if each[-3:] in ['mp4','avi'] :  \r\n        os.system('ffmpeg.exe -i %s -c copy -vbsf h264_mp4toannexb %s.ts' % (each, each[:-4]))  \r\n        listalista=lista +('|%s.ts'%(each[:-4]))  \r\nlistalista=lista.replace('kaishiwizhi|','')  \r\nimport subprocess,cv2  \r\n#subprocess.call(cmd, shell=True)  \r\ncom = f'ffmpeg -i \"concat:%s\" -c copy 333333.avi'%lista  \r\n#com = f'mencoder -forceidx -of lavf -oac copy -ovc copy -o output.avinew 2.avi new 2.avi'  \r\nprint(com)  \r\nos.system(com)  \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u901a\u8fc7python\u7684opencv\u5e93\u5c31\u53ef\u4ee5\u628a\u56fe\u7247\u8f6c\u6210\u89c6\u9891\uff0c\u8981\u6ce8\u610f\u7684\u662f\u628a\u5e27\u7387\u8bbe\u7f6e\u621030\uff0c\u65b9\u4fbf\u540e\u7eed\u548c\u5176\u4ed6\u89c6\u9891\u62fc\u63a5\uff0c\u56e0\u4e3a […]<\/p>\n","protected":false},"author":1479,"featured_media":222863,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-222859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=222859"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222859\/revisions"}],"predecessor-version":[{"id":222864,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222859\/revisions\/222864"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/222863"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=222859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=222859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=222859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}