Title: work with numpy · python-ffmpegio/python-ffmpegio · Discussion #37 · GitHub
Open Graph Title: work with numpy · python-ffmpegio/python-ffmpegio · Discussion #37
X Title: work with numpy · python-ffmpegio/python-ffmpegio · Discussion #37
Description: work with numpy
Open Graph Description: objective work with numpy to give filter in this example below is to make video in grayscale steps !wget -c https://cdn.creatomate.com/demo/mountains.mp4 !wget -c http://www.jsayles.com/music/sheep...
X Description: objective work with numpy to give filter in this example below is to make video in grayscale steps !wget -c https://cdn.creatomate.com/demo/mountains.mp4 !wget -c http://www.jsayles.com/music/sheep...
Opengraph URL: https://github.com/python-ffmpegio/python-ffmpegio/discussions/37
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"work with numpy","text":"\u003cp dir=\"auto\"\u003e\u003cstrong\u003eobjective\u003c/strong\u003e\u003cbr\u003e\nwork with numpy to give filter in this example below is to make video in grayscale\u003c/p\u003e\n\u003cp dir=\"auto\"\u003e\u003cstrong\u003esteps\u003c/strong\u003e\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"!wget -c https://cdn.creatomate.com/demo/mountains.mp4\n!wget -c http://www.jsayles.com/music/sheep.mp3\npip install -U ffmpegio\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode class=\"notranslate\"\u003e!wget -c https://cdn.creatomate.com/demo/mountains.mp4\n!wget -c http://www.jsayles.com/music/sheep.mp3\npip install -U ffmpegio\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003cstrong\u003ecode\u003c/strong\u003e\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"import ffmpegio\nimport os\nimport subprocess\nimport numpy as np\nwith ffmpegio.open('mountains.mp4', 'rv') as fin, ffmpegio.open('temp.mp4', 'wv', rate_in = fin.rate, overwrite = True) as fout:\n for frame in fin:\n grayscale= np.dot(frame[0,...], [0.299, 0.587, 0.144] )\n fout.write(grayscale)\nsubprocess.check_call(\n [\n \u0026quot;ffmpeg\u0026quot;,\n \u0026quot;-y\u0026quot;,\n \u0026quot;-i\u0026quot;,\n 'temp.mp4',\n \u0026quot;-i\u0026quot;,\n 'sheep.mp3',\n \u0026quot;-r\u0026quot;,\n '10',\n \u0026quot;-vcodec\u0026quot;,\n \u0026quot;libx264\u0026quot;,\n \u0026quot;-acodec\u0026quot;,\n \u0026quot;copy\u0026quot;, # aac (smallest), libmp3lame, libtwolame (biggest), copy (faster because not reencoding)\n \u0026quot;-shortest\u0026quot;,\n 'grayscale.mp4',\n ]\n)\nos.remove(temp)\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode class=\"notranslate\"\u003eimport ffmpegio\nimport os\nimport subprocess\nimport numpy as np\nwith ffmpegio.open('mountains.mp4', 'rv') as fin, ffmpegio.open('temp.mp4', 'wv', rate_in = fin.rate, overwrite = True) as fout:\n for frame in fin:\n grayscale= np.dot(frame[0,...], [0.299, 0.587, 0.144] )\n fout.write(grayscale)\nsubprocess.check_call(\n [\n \"ffmpeg\",\n \"-y\",\n \"-i\",\n 'temp.mp4',\n \"-i\",\n 'sheep.mp3',\n \"-r\",\n '10',\n \"-vcodec\",\n \"libx264\",\n \"-acodec\",\n \"copy\", # aac (smallest), libmp3lame, libtwolame (biggest), copy (faster because not reencoding)\n \"-shortest\",\n 'grayscale.mp4',\n ]\n)\nos.remove(temp)\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003cstrong\u003eresult\u003c/strong\u003e\u003c/p\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"'\u0026lt;f8'\n---------------------------------------------------------------------------\nKeyError Traceback (most recent call last)\n[/usr/local/lib/python3.10/dist-packages/ffmpegio/utils/__init__.py](https://localhost:8080/#) in guess_video_format(shape, dtype)\n 426 try:\n--\u0026gt; 427 pix_fmt = {\n 428 \u0026quot;|u1\u0026quot;: {1: \u0026quot;gray\u0026quot;, 2: \u0026quot;ya8\u0026quot;, 3: \u0026quot;rgb24\u0026quot;, 4: \u0026quot;rgba\u0026quot;},\n\nKeyError: '\u0026lt;f8'\n\nDuring handling of the above exception, another exception occurred:\n\nValueError Traceback (most recent call last)\n4 frames\n[/usr/local/lib/python3.10/dist-packages/ffmpegio/utils/__init__.py](https://localhost:8080/#) in guess_video_format(shape, dtype)\n 432 except Exception as e:\n 433 print(e)\n--\u0026gt; 434 raise ValueError(\n 435 f\u0026quot;dtype ({dtype}) and guessed number of components ({ncomp}) do not yield a pix_fmt.\u0026quot;\n 436 )\n\nValueError: dtype (\u0026lt;f8) and guessed number of components (1) do not yield a pix_fmt.\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode class=\"notranslate\"\u003e'\u0026lt;f8'\n---------------------------------------------------------------------------\nKeyError Traceback (most recent call last)\n[/usr/local/lib/python3.10/dist-packages/ffmpegio/utils/__init__.py](https://localhost:8080/#) in guess_video_format(shape, dtype)\n 426 try:\n--\u0026gt; 427 pix_fmt = {\n 428 \"|u1\": {1: \"gray\", 2: \"ya8\", 3: \"rgb24\", 4: \"rgba\"},\n\nKeyError: '\u0026lt;f8'\n\nDuring handling of the above exception, another exception occurred:\n\nValueError Traceback (most recent call last)\n4 frames\n[/usr/local/lib/python3.10/dist-packages/ffmpegio/utils/__init__.py](https://localhost:8080/#) in guess_video_format(shape, dtype)\n 432 except Exception as e:\n 433 print(e)\n--\u0026gt; 434 raise ValueError(\n 435 f\"dtype ({dtype}) and guessed number of components ({ncomp}) do not yield a pix_fmt.\"\n 436 )\n\nValueError: dtype (\u0026lt;f8) and guessed number of components (1) do not yield a pix_fmt.\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003ebest regards\u003c/p\u003e","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eShort answer: \u003ccode class=\"notranslate\"\u003egrayscale= np.dot(frame[0,...], [0.299, 0.587, 0.144] ).astype(np.uint8)\u003c/code\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou're responsible to put the input Numpy array in the correct image pixel format. \u003ccode class=\"notranslate\"\u003effmpegio\u003c/code\u003e simply passes the numpy data bytes straight to FFmpeg. Assuming that the video is RGB24 (8-bit per color channel) your input data dtype must be either \u003ccode class=\"notranslate\"\u003e\"\u0026lt;u1\"\u003c/code\u003e or \u003ccode class=\"notranslate\"\u003enp.uint8\u003c/code\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eNow, if you're performing these fixed function (the same transform on every frame) read multiple frames at a time (as many as your system can handle). Looping one frame at a time significantly slows down the process.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFinally, as I alluded in the other post of yours, I'm a big proponent of using the FFmpeg filters whenever possible. In this case, \u003ca href=\"https://ffmpeg.org/ffmpeg-filters.html#colorchannelmixer\" rel=\"nofollow\"\u003ecolorchennelmixer\u003c/a\u003e does the job with \u003ccode class=\"notranslate\"\u003effmpegio.transpose\u003c/code\u003e. Something along the line of\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"ffmpegio.transpose('mountains.mp4', 'temp.mp4', overwrite = True, vf='colorchannelmixer=0.299:0.587:0.144:00.299:0.587:0.144:00.299:0.587:0.144:0')\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-s1\"\u003effmpegio\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003etranspose\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e'mountains.mp4'\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e'temp.mp4'\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eoverwrite\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eTrue\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003evf\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e'colorchannelmixer=0.299:0.587:0.144:00.299:0.587:0.144:00.299:0.587:0.144:0'\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eI have not tested this command, but you get the idea. If you want to apply multiple effects (to grayscale + flip) then you can chain multiple filters as needed.\u003c/p\u003e","upvoteCount":1,"url":"https://github.com/python-ffmpegio/python-ffmpegio/discussions/37#discussioncomment-8777987"}}}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:317c2e8a-2328-0b54-2224-fcb9bf58fd4d |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | E40C:3073EF:277E8E:375B61:6AFD7390 |
| html-safe-nonce | fd32c4015529a769860a04331dfb95743695b4bfac1700d4651df4a73237d5dd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDBDOjMwNzNFRjoyNzdFOEU6Mzc1QjYxOjZBRkQ3MzkwIiwidmlzaXRvcl9pZCI6IjQ5ODY4MDkxNzY4MTk1OTQxMjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e3345026c1a7813852d3bac43a4d91aa3f9b7fde0174e200a024bb84b4ca305d |
| hovercard-subject-tag | discussion:6369214 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python-ffmpegio/python-ffmpegio/37/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/90f7acae59a642fa59239b987d44298127b701b067eb2d98b60b82f8f3818e74/python-ffmpegio/python-ffmpegio/discussions/37 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90f7acae59a642fa59239b987d44298127b701b067eb2d98b60b82f8f3818e74/python-ffmpegio/python-ffmpegio/discussions/37 |
| og:image:alt | objective work with numpy to give filter in this example below is to make video in grayscale steps !wget -c https://cdn.creatomate.com/demo/mountains.mp4 !wget -c http://www.jsayles.com/music/sheep... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | e54006cf2b96d1ac260960edd10003132911469ccd1ec99c9803119fe2a4e032 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-ffmpegio/python-ffmpegio git https://github.com/python-ffmpegio/python-ffmpegio.git |
| octolytics-dimension-user_id | 98168493 |
| octolytics-dimension-user_login | python-ffmpegio |
| octolytics-dimension-repository_id | 355600490 |
| octolytics-dimension-repository_nwo | python-ffmpegio/python-ffmpegio |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 355600490 |
| octolytics-dimension-repository_network_root_nwo | python-ffmpegio/python-ffmpegio |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5be0bf2b8cf26f8897d31e6bc3c232d328b12a54 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width