<h3 dir="auto">System Information</h3>
<div class="highlight highlight-text-adblock notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="opencv-python==4.7.0.68
Python 3.10.9
Arch Linux"><pre class="notranslate">opencv-python==4.7.0.68
Python 3.10.9
Arch Linux</pre></div>
<h3 dir="auto">Detailed description</h3>
<p dir="auto"><code class="notranslate">VideoWriter</code> fails silently when the size of the frame passed to <code class="notranslate">VideoWriter().write()</code> differs from the <code class="notranslate">frameSize</code> specified in the constructor.</p>
<p dir="auto">I would expect an error, but instead, the written video will not play, which is hard to trace back to a frame size issue.</p>
<p dir="auto">Related:<br>
<a href="https://stackoverflow.com/questions/75000164/cant-play-video-created-with-opencv-videowriter" rel="nofollow">https://stackoverflow.com/questions/75000164/cant-play-video-created-with-opencv-videowriter</a></p>
<h3 dir="auto">Steps to reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import cv2


FPS = 30
KEY_ESC = 27
OUTPUT_FILE = &quot;vid.mp4&quot;

cam = cv2.VideoCapture(0)

codec = cv2.VideoWriter.fourcc(*&quot;mp4v&quot;) # MPEG-4 http://mp4ra.org/#/codecs
frame_size = cam.read()[1].shape[:2] # wrong (see SO link)
video_writer = cv2.VideoWriter(OUTPUT_FILE, codec, FPS, frame_size)

# record until user exits with ESC
while True:
    success, image = cam.read()
    cv2.imshow(&quot;window&quot;, image)

    video_writer.write(image)

    if cv2.waitKey(5) == KEY_ESC:
        break"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">cv2</span>


<span class="pl-v">FPS</span> <span class="pl-c1">=</span> <span class="pl-c1">30</span>
<span class="pl-v">KEY_ESC</span> <span class="pl-c1">=</span> <span class="pl-c1">27</span>
<span class="pl-v">OUTPUT_FILE</span> <span class="pl-c1">=</span> <span class="pl-s">"vid.mp4"</span>

<span class="pl-s1">cam</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">VideoCapture</span>(<span class="pl-c1">0</span>)

<span class="pl-s1">codec</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">VideoWriter</span>.<span class="pl-en">fourcc</span>(<span class="pl-c1">*</span><span class="pl-s">"mp4v"</span>) <span class="pl-c"># MPEG-4 http://mp4ra.org/#/codecs</span>
<span class="pl-s1">frame_size</span> <span class="pl-c1">=</span> <span class="pl-s1">cam</span>.<span class="pl-en">read</span>()[<span class="pl-c1">1</span>].<span class="pl-s1">shape</span>[:<span class="pl-c1">2</span>] <span class="pl-c"># wrong (see SO link)</span>
<span class="pl-s1">video_writer</span> <span class="pl-c1">=</span> <span class="pl-s1">cv2</span>.<span class="pl-v">VideoWriter</span>(<span class="pl-v">OUTPUT_FILE</span>, <span class="pl-s1">codec</span>, <span class="pl-v">FPS</span>, <span class="pl-s1">frame_size</span>)

<span class="pl-c"># record until user exits with ESC</span>
<span class="pl-k">while</span> <span class="pl-c1">True</span>:
    <span class="pl-s1">success</span>, <span class="pl-s1">image</span> <span class="pl-c1">=</span> <span class="pl-s1">cam</span>.<span class="pl-en">read</span>()
    <span class="pl-s1">cv2</span>.<span class="pl-en">imshow</span>(<span class="pl-s">"window"</span>, <span class="pl-s1">image</span>)

    <span class="pl-s1">video_writer</span>.<span class="pl-en">write</span>(<span class="pl-s1">image</span>)

    <span class="pl-k">if</span> <span class="pl-s1">cv2</span>.<span class="pl-en">waitKey</span>(<span class="pl-c1">5</span>) <span class="pl-c1">==</span> <span class="pl-v">KEY_ESC</span>:
        <span class="pl-k">break</span></pre></div>
<h3 dir="auto">Issue submission checklist</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I report the issue, it's not a question</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I updated to the latest OpenCV version and the issue is still there</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> There is reproducer code and related data files (videos, images, onnx, etc)</li>
</ul>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; <g-emoji class="g-emoji" alias="grey_question" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2754.png">❔</g-emoji></li>
<li>Operating System / Platform =&gt; ❔</li>
<li>Compiler =&gt; <g-emoji class="g-emoji" alias="grey_question" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2754.png">❔</g-emoji></li>
</ul>
<h5 dir="auto">Detailed description</h5>

<h5 dir="auto">Steps to reproduce</h5>