{"id":105,"date":"2022-09-24T21:45:28","date_gmt":"2022-09-24T21:45:28","guid":{"rendered":"https:\/\/blog.wordgeeks.net\/?p=105"},"modified":"2022-09-24T21:45:28","modified_gmt":"2022-09-24T21:45:28","slug":"random-debugging-video-related","status":"publish","type":"post","link":"https:\/\/blog.wordgeeks.net\/?p=105","title":{"rendered":"Random Debugging: video related"},"content":{"rendered":"\n<p>I&#8217;m trying to use my Raspberry Pi as a home monitoring system, together with a webcam.<\/p>\n\n\n\n<p>Over the past few weeks or so, I encountered multiple problems, each of which wasn&#8217;t complicated, but due to the lack of documentation and\/or proper error message, they took a long time to resolve.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Video not playable<ul><li>Part of the setup was just to read from the webcam frame by frame, then write the frames to a video file, pretty much like <a href=\"https:\/\/gist.github.com\/boyou\/580bbdc0fedb7af5446ef5b8ccf4ce8f\">https:\/\/gist.github.com\/boyou\/580bbdc0fedb7af5446ef5b8ccf4ce8f<\/a>.<\/li><li>No error\/warning was raised, a video file was generated, but it couldn&#8217;t be played by VLC or the browser.<\/li><li>I wasn&#8217;t so familiar with <code>ffmpeg<\/code> \/ <code>ffprob <\/code>\/ etc, so I ended up eyeballing and trying random things like different codecs, and finally found out that <a href=\"https:\/\/docs.opencv.org\/3.4\/d6\/d50\/classcv_1_1Size__.html\">the size parameter to <code>VideoWriter<\/code> is <code>(width, height)<\/code><\/a>, while the <code>ndarray<\/code> is in row first order.<\/li><li>I could have used things like <code>ffmpeg -i tmp.mp4<\/code> or <code>ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 tmp.mp4<\/code>, and noticed the video file is indeed malformed.<\/li><li>But anyway, opencv should have raised an error or warning.<\/li><\/ul><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>Low frame rate of video<ul><li>I was trying to let <code>ffmpeg<\/code> read the webcam and periodically write to video files, with command like <a href=\"https:\/\/gist.github.com\/boyou\/cf4f210859ab3137cddf73da6df61100\">this<\/a>.<\/li><li>However, the frame rate was very low (10), instead of the specified 30.<\/li><li>Luckily this time there was some useful log, &#8220;The driver changed the time per frame from 1\/30 to 1\/10&#8221;.<\/li><li>Some search lead me to <a href=\"https:\/\/stackoverflow.com\/a\/44960816\">this answer<\/a>, so we need to <a href=\"https:\/\/gist.github.com\/boyou\/bc7a029a6d9a1fb56bc84f507e034911\">configure the codec twice, both to <code>mjpeg<\/code><\/a>.<\/li><\/ul><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/21810452\/cv2-imshow-command-doesnt-work-properly-in-opencv-python\"><code>cv2.imshow<\/code> has to be followed by <code>cv2.waitKey<\/code>.<\/a><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>The almight <code>ffmpeg<\/code><ul><li>As mentioned earlier, <a href=\"https:\/\/stackoverflow.com\/questions\/33214300\/ffmpeg-create-contiguous-videos-from-webcam\">continuous video from webcam<\/a>.<\/li><li><a href=\"https:\/\/unix.stackexchange.com\/questions\/1670\/how-can-i-use-ffmpeg-to-split-mpeg-video-into-10-minute-chunks\">Split video into chunks<\/a>.<\/li><li><a href=\"https:\/\/linuxhint.com\/cut-and-crop-a-video-with-ffmpeg\/\">Crop or cut the video<\/a>.<ul><li><a href=\"https:\/\/superuser.com\/questions\/685562\/cutting-videos-with-ffmpeg-length-not-accurate\">There are some subtleties that can cause the time to be inaccurate<\/a>. We can either don&#8217;t use <code>copy<\/code> or put <code>ss<\/code>\/<code>t<\/code> options AFTER the input.<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to use my Raspberry Pi as a home monitoring system, together with a webcam. Over the past few weeks or so, I encountered multiple problems, each of which wasn&#8217;t complicated, but due to the lack of documentation and\/or proper error message, they took a long time to resolve. Video not playable Part of&hellip; <a class=\"more-link\" href=\"https:\/\/blog.wordgeeks.net\/?p=105\">Continue reading <span class=\"screen-reader-text\">Random Debugging: video related<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[20,3],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-random-debugging-notes","tag-audio-video","tag-programming","entry"],"_links":{"self":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":1,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wordgeeks.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}