1. 程式人生 > >止步於磁盤空間不足

止步於磁盤空間不足

user art sse sas object projects from highlight .org

C:\Users\sas\PycharmProjects\produce_video\mypng\a (1).jpg
OpenCV Error: Assertion failed (wsz == wsz0) in cv::mjpeg::BitStream::writeBlock, file C:\projects\opencv-python\opencv\modules\videoio\src\cap_mjpeg_encoder.cpp, line 135
Traceback (most recent call last):
  File "C:/Users/sas/PycharmProjects/produce_video/myt.py", line 226, in <module>
    gen_video(os_delay_factor=1, mystep=0.005,bear_error_second=0.5, audio_spend=58.777058839797974)
  File "C:/Users/sas/PycharmProjects/produce_video/myt.py", line 219, in gen_video
    gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=58.777058839797974)
  File "C:/Users/sas/PycharmProjects/produce_video/myt.py", line 219, in gen_video
    gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=58.777058839797974)
  File "C:/Users/sas/PycharmProjects/produce_video/myt.py", line 219, in gen_video
    gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=58.777058839797974)
  [Previous line repeated 21 more times]
  File "C:/Users/sas/PycharmProjects/produce_video/myt.py", line 198, in gen_video
    videoWriter.write(img)
cv2.error: C:\projects\opencv-python\opencv\modules\videoio\src\cap_mjpeg_encoder.cpp:135: error: (-215) wsz == wsz0 in function cv::mjpeg::BitStream::writeBlock

OpenCV Error: Assertion failed (wsz == wsz0) in cv::mjpeg::BitStream::writeBlock, file C:\projects\opencv-python\opencv\modules\videoio\src\cap_mjpeg_encoder.cpp, line 135

  

技術分享圖片

import os


import os, time, glob
import cv2

os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[
    -1]

f_img_d = ‘{}{}{}{}{}‘.format(this_file_dirname, os_sep, ‘mypng‘, os_sep, ‘*.jpg‘)
imgs, img_size_d = glob.glob(f_img_d), {}
for i in imgs:
    img = cv2.imread(i)
    w_h_s = ‘{},{}‘.format(img.shape[1], img.shape[0])
    if w_h_s not in img_size_d:
        img_size_d[w_h_s] = 1
    else:
        img_size_d[w_h_s] += 1

mode_img_size_wh = [int(i) for i in
                    sorted(img_size_d.items(), key=lambda img_size_d: img_size_d[0], reverse=True)[0][0].split(‘,‘)]

import os

os_sep = os.sep
this_file_abspath = os.path.abspath(__file__)
this_file_dirname, this_file_name = os.path.dirname(this_file_abspath), os.path.abspath(__file__).split(os_sep)[-1]

f_mp3 = ‘{}{}{}‘.format(this_file_dirname, os_sep, ‘auido.mp3‘)
from playsound import playsound

import time
import math

this_time = time.time()

# playsound(f_mp3)

# t_spend = time.time() - this_time
t_spend = 58.777058839797974
# 音頻的秒數
t_spend = math.ceil(t_spend)
import cv2
import glob

‘‘‘
python+opencv視頻圖像相互轉換 - CSDN博客 https://blog.csdn.net/m0_37733057/article/details/79023693
鏈接:https://www.zhihu.com/question/49558804/answer/343058915

OpenCV: Drawing Functions in OpenCV https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

‘‘‘
# 每秒傳輸幀數(Frames Per Second)
fps = 100  # 保存視頻的FPS,可以適當調整 FPS是圖像領域中的定義,是指畫面每秒傳輸幀數,通俗來講就是指動畫或視頻的畫面數。FPS是測量用於保存、顯示動態視頻的信息數量。每秒鐘幀數愈多,所顯示的動作就會愈流暢。通常,要避免動作不流暢的最低是30。某些計算機視頻格式,每秒只能提供15幀。

f_img_d = ‘{}{}{}{}{}‘.format(this_file_dirname, os_sep, ‘mypng‘, os_sep, ‘*.jpg‘)
imgs = glob.glob(f_img_d)

"""
用圖片總數均分音頻時間
"""
os_delay_factor = 0.14
os_delay_factor = 0.11
myinterval = t_spend / len(imgs) * os_delay_factor

f, l = ‘mybaidu.parp.txt‘, []
with open(f, ‘r‘, encoding=‘utf-8‘) as fr:
    for i in fr:
        ii = i.replace(‘\n‘, ‘‘)
        l.append(ii)
char_loop_l = []
for i in l:
    mystr, le = ‘‘, len(i)
    for ii in range(le):
        iii = i[ii]
        print(‘-----‘, iii)
        mystr = ‘{}{}‘.format(mystr, iii)
        print(mystr)
        char_loop_l.append(iii)


#
# from fontTools.ttLib import TTFont
# myfont = TTFont(‘simhei.ttf‘)


def resize_rescale_pilimg(img_f, w_h_tuple=(mode_img_size_wh[0], mode_img_size_wh[1]), mid_factor=1):
    img_n, img_type = img_f.split(‘.‘)[-2], img_f.split(‘.‘)[-1]
    img_type = ‘PNG‘
    img_f_new = ‘{}{}{}{}‘.format(img_n,int(time.time()), ‘resize_rescale.‘, img_type)
    mid_icon = Image.open(img_f)
    mid_icon_w, mid_icon_h = w_h_tuple[0] * mid_factor, w_h_tuple[1] * mid_factor
    mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS)
    mid_icon.save(img_f_new, img_type)
    return img_f_new


def compute_video_playtime(f):
    # 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(f)

    # 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:

            # Display the resulting frame
            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()

    return time.time() - this_time


from PIL import Image, ImageDraw, ImageFont

myfont = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8")

import cv2
import numpy as np


def gen_video(os_delay_factor=0.245, mystep=0.01,bear_error_second=1, audio_spend=58.777058839797974):
    f_v = ‘{}{}‘.format(int(time.time()), ‘saveVideo.avi‘)
    fps, fourcc = 15, cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘)
    # fourcc = cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘)  # opencv3.0
    videoWriter = cv2.VideoWriter(f_v, fourcc, fps, (mode_img_size_wh[0], mode_img_size_wh[1]))

    f_v = ‘{}{}‘.format(int(time.time()), ‘saveVideo.avi‘)
    myinterval = t_spend / (len(char_loop_l) * 1) * os_delay_factor
    for i in l:
        i_index = l.index(i)
        img_index = i_index % len(imgs)
        imgname = imgs[img_index]

        mystr, le = ‘‘, len(i)
        for ii in range(le):
            iii = i[ii]
            print(‘-----‘, iii)
            if len(mystr) % 15 == 0:
                mystr = ‘{}{}‘.format(mystr, ‘\n‘)
            mystr = ‘{}{}‘.format(mystr, iii)
            print(mystr)

            this_time = time.time()
            while time.time() - this_time < myinterval:
                print(imgname)

                frame = cv2.imread(imgname)
                del_f=False
                if (frame.shape[1], frame.shape[0]) != (mode_img_size_wh[0], mode_img_size_wh[1]):
                    imgname = resize_rescale_pilimg(imgname)
                    frame = cv2.imread(imgname)

                    del_f = True
                else:
                    pass

                frame_cv2 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                frame_pil = Image.fromarray(frame_cv2)  # 轉為PIL的圖片格式


                font = ImageFont.truetype("simhei.ttf", 50, encoding="utf-8")
                # 第一個參數為字體,中文黑體
                # 第二個為字體大小
                ImageDraw.Draw(frame_pil).text((100, 20), mystr, (0, 0, 255), font)

                ‘‘‘
                    frame_pil:目標圖像
                    第一個參數為打印的坐標
                    第二個為打印的文本
                    第三個為字體顏色
                    第四個為字體
                ‘‘‘

                frame_cv2 = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
                img = frame_cv2
                videoWriter.write(img)

                if del_f:
                    if os.path.exists(imgname):
                        print(imgname)
                        print(‘del‘)
                        # 刪除文件,可使用以下兩種方法。
                        os.remove(imgname)
                        # os.unlink(my_file)
                else:
                    pass


    videoWriter.release()

    time.sleep(3)

    video_playtime = compute_video_playtime(f_v)

    if video_playtime - audio_spend > bear_error_second:
        os_delay_factor -= mystep
        gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=58.777058839797974)
    elif  audio_spend- video_playtime > bear_error_second:
        os_delay_factor += mystep
        gen_video(os_delay_factor=os_delay_factor, mystep=0.005, audio_spend=58.777058839797974)
    else:
        os._exit(123)

gen_video(os_delay_factor=1, mystep=0.005,bear_error_second=0.5, audio_spend=58.777058839797974)


‘‘‘
 

‘‘‘

from PIL import Image


#
#
# logo = ‘fugu.png‘
# mid_icon = Image.open(logo)
# mid_icon_w, mid_icon_h = mid_icon.size
# mid_factor = 5
# mid_icon_w, mid_icon_h = mid_icon_w * mid_factor, mid_icon_h * mid_factor
# mid_icon = mid_icon.resize((mid_icon_w * 3, mid_icon_h), Image.ANTIALIAS)
# mid_icon.save(‘fugu.2.png‘, ‘PNG‘)

  

止步於磁盤空間不足