{"id":20130,"date":"2015-10-20T10:59:18","date_gmt":"2015-10-20T16:29:18","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/how-to-get-video-details-including-orientation-duration-using-ffmpeg\/"},"modified":"2023-10-27T05:29:42","modified_gmt":"2023-10-27T10:59:42","slug":"how-to-get-video-details-including-orientation-duration-using-ffmpeg","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/how-to-get-video-details-including-orientation-duration-using-ffmpeg\/","title":{"rendered":"How to Get Video Details Including Orientation Duration Using FFMPEG"},"content":{"rendered":"<div style=\"text-align: justify;\">Here I am going to get details related to FFMPEG including orientation, duration.<\/div>\n<p>Step to get details related to <a href=\"https:\/\/www.ffmpeg.org\/\" target=\"_blank\" rel=\"noopener\">FFMPEG<\/a>including orientation, duration.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Get_Video_duration\"><\/span><strong>1. Get Video duration:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"color: #0000ff;\">$ffmpeg_path = &#8220;\/usr\/bin\/ffmpeg&#8221;; \/\/Path to your FFMPEG<\/span><\/p>\n<div><span style=\"color: #0000ff;\">$video_path = &#8220;\/vidoes\/myvideo.mov&#8221;; \/\/\u00a0Path to your Video<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #0000ff;\">$command = $ffmpeg_path .\u00a0&#8216; -i &#8220;&#8216;\u00a0. $video_path\u00a0.\u00a0&#8216;&#8221; -vstats 2&gt;&amp;1&#8217;;<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #0000ff;\">$output = shell_exec($command);<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$regex_duration =\u00a0&#8220;\/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})\/&#8221;;<\/span><\/div>\n<div><span style=\"color: #0000ff;\">if\u00a0(preg_match($regex_duration, $output, $regs)) {<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$hours = $regs [1] ? $regs [1] :\u00a0null;<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$mins = $regs [2] ? $regs [2] :\u00a0null;<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$secs = $regs [3] ? $regs [3] :\u00a0null;<\/span><\/div>\n<div><span style=\"color: #0000ff;\">}<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #0000ff;\">$video_Length = $hours . &#8220;:&#8221; . $mins . &#8220;:&#8221; . $secs;<\/span><\/div>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Get_Video_thumbnail\"><\/span><strong>2. Get Video thumbnail:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"color: #0000ff;\">$save_video_thumbnail = &#8216;thumbnails\/myvideo_thumbnail&#8217;;<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">$cmd =\u00a0&#8220;$ffmpeg_path\u00a0-i\u00a0$video_path\u00a0-ss 00:00:03 -s 404&#215;694 -vframes 1\u00a0$save_video_thumbnail&#8221;;<\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div>\n<p><span style=\"color: #0000ff;\">$output = shell_exec($command);<\/span><\/p>\n<p><strong>Notes<\/strong>: In Above\u00a000:00:03 is duration when thumbnail is generated. We can set this as per requirement.<\/p>\n<p><strong>3. Get Video Orientation:<\/strong><\/p>\n<p><span style=\"color: #0000ff;\">$ffmpeg_path =\u00a0&#8220;\/usr\/bin\/ffmpeg&#8221;; \/\/Path to your FFMPEG<br \/>\n$video_path = &#8220;\/vidoes\/myvideo.mov&#8221;; \/\/\u00a0Path to your Video<\/span><\/p>\n<div><span style=\"color: #0000ff;\">$command =\u00a0$ffmpeg_path .\u00a0&#8216; -i &#8220;&#8216;\u00a0. $video_path\u00a0.\u00a0&#8216;&#8221; -vstats 2&gt;&amp;1&#8217;;<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #0000ff;\">$output = shell_exec($command);<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #0000ff;\">$out_arr = explode(&#8220;n&#8221;, $output);<\/span><\/div>\n<div><span style=\"color: #0000ff;\">foreach($out_arr\u00a0as\u00a0$line) {<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">if( preg_match(&#8216;\/^Stream.*Video:\/&#8217;, trim($line)) ) {<\/span><\/div>\n<div><span style=\"color: #0000ff;\">\/* match line: Stream #0.0(und): Video: h264 (High), yuv420p, 640&#215;360 [PAR 1:1 DAR 16:9], 597 kb\/s, 25 fps, 25 tbr, 25k tbn, 50 tbc *\/<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$line_arr = explode(&#8216;,&#8217;, $line);<\/span><\/div>\n<div><span style=\"color: #0000ff;\">\/\/ get field: 640&#215;360 [PAR 1:1 DAR 16:9]<\/span><\/div>\n<div><span style=\"color: #0000ff;\">$target_arr = explode(&#8216; &#8216;, $line_arr[3]);<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">\/\/ get parts: 640&#215;360<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">$dims = explode(&#8216;x&#8217;, $target_arr[1]);<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">$res_x = $dims[0];<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">$res_y = $dims[1];<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">}<\/span><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">}<\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\"><span style=\"color: #0000ff;\">$orientation = (intval($res_x) &gt; intval($res_y)) ?\u00a0&#8216;Portrait&#8217;\u00a0:&#8217;Landscape&#8217;;<\/span><\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">You can <a href=\"mailto:info@solutionanalysts.com?subject=How%20to%20get%20Video%20Details%20including%20orientation%20duration%20Using%20FFMPEG%20%3F\">drop message<\/a> for any questions or feedback. Will get back to you soon.<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here I am going to get details related to FFMPEG including orientation, duration. Step to get details related to FFMPEGincluding orientation, duration. 1. Get Video duration: $ffmpeg_path = &#8220;\/usr\/bin\/ffmpeg&#8221;; \/\/Path to your FFMPEG $video_path = &#8220;\/vidoes\/myvideo.mov&#8221;; \/\/\u00a0Path to your Video $command = $ffmpeg_path .\u00a0&#8216; -i &#8220;&#8216;\u00a0. $video_path\u00a0.\u00a0&#8216;&#8221; -vstats 2&gt;&amp;1&#8217;; $output = shell_exec($command); $regex_duration =\u00a0&#8220;\/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})\/&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20131,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-20130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hire-developer"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20130"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20130\/revisions"}],"predecessor-version":[{"id":32600,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20130\/revisions\/32600"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20131"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}