LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2010, 05:43 PM   #1
toben
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 0
Question Use awk and sed to extract height and width of file


Hello I am trying to write commands that extracts the height and width of a video file via ffmpeg. I have the following working so far:

ffmpeg -i videofile.mov 2>&1 |grep Video|awk '{print $6}'

This gives the following answer in widthxheight format with an extra ,
720x480,

How can I instead run 2 separate commands that give me height and width separately? I want some command to give me 720 and another command to give me 480 and I dont need the x or the ,

Any suggestions?


If you need to know this is what ffmpeg -i videofile.mov 2>&1 gives as output

ffmpeg -i videofile.mov 2>&1

FFmpeg version SVN-r23940, Copyright (c) 2000-2010 the FFmpeg developers
built on Jul 1 2010 19:57:32 with gcc 4.4.3
configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
libavutil 50.19. 1 / 50.19. 1
libavcodec 52.78. 1 / 52.78. 1
libavformat 52.71. 0 / 52.71. 0
libavdevice 52. 2. 0 / 52. 2. 0
libavfilter 1.20. 1 / 1.20. 1
libswscale 0.11. 0 / 0.11. 0
libpostproc 51. 2. 0 / 51. 2. 0
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x1b7f470] max_analyze_duration reached

Seems stream 0 codec frame rate differs from container frame rate: 5994.00 (5994/1) -> 29.97 (2997/100)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'mydc3756.mov':
Metadata:
major_brand : qt
minor_version : 537199360
compatible_brands: qt
Duration: 00:28:30.14, start: 0.000000, bitrate: 2397 kb/s
Stream #0.0(eng): Video: h264, yuv420p, 720x480, 1985 kb/s, PAR 10000:11250 DAR 4:3, 29.97 fps, 29.97 tbr, 2997 tbn, 5994 tbc
Stream #0.1(eng): Audio: adpcm_ima_qt, 48000 Hz, 2 channels, s16
Stream #0.2(eng): Data: tmcd / 0x64636D74
At least one output file must be specified

Last edited by toben; 08-03-2010 at 05:47 PM.
 
Old 08-03-2010, 06:39 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
One, not very elegant, way of doing it would be:
Code:
ffmpeg -i marcus.avi 2>&1 | awk '/Video/ {print $6}' | awk -Fx '{print $1}'
Code:
ffmpeg -i marcus.avi 2>&1 | awk '/Video/ {print $6}' | awk -Fx '{print $2}'

Last edited by sycamorex; 08-03-2010 at 07:10 PM.
 
Old 08-03-2010, 06:49 PM   #3
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Using your output file, I was able to get what, I thought you said you wanted, by doing as follows.

The command sequence:

Code:
ffmpeg -i videofile.mov 2>&1 |grep Video|awk '{ split( $6, pieces,  /[x,]/ ) ; print pieces[1] }'
outputs:

720


The command sequence:

Code:
ffmpeg -i videofile.mov 2>&1 |grep Video|awk '{ split( $6, pieces,  /[x,]/ ) ; print pieces[2] }'
outputs:

480
 
Old 08-03-2010, 07:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps combine the two responses - no need for that grep.
 
Old 08-03-2010, 08:47 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
with bash 3.2+

Code:
var=$(ffmpeg -i videofile.mov 2>&1)
[[ $var =~ ".*([0-9]{3})x([0-9]{3}).*" ]]
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
 
Old 08-04-2010, 05:06 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about:
Code:
var=($(ffmpeg -i  videofile.mov 2>&1 | awk '/Video/{print gensub(/.* ([0-9]+)x([0-9]+) .*/,"\\1\n\\2","g")}'))

echo ${var[0]}
echo ${var[1]}
 
Old 08-04-2010, 06:16 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just an alternative if mplayer is an option:
Code:
var=($(mplayer -identify -frames 0 videofile.mov 2>&1 | awk -F= '/WIDTH|HEIGHT/{print $2}'))
echo ${var[0]}
echo ${var[1]}
or in two different variables
Code:
wdt=$(mplayer -identify -frames 0 videofile.mov 2>&1 | awk -F= '/WIDTH/{print $2}')
hgt=$(mplayer -identify -frames 0 videofile.mov 2>&1 | awk -F= '/HEIGHT/{print $2}')
I was looking at this because the option -i of ffmpeg has another meaning on my system.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get width/height of a terminal window in c++? rohedin Programming 9 10-24-2012 07:00 PM
imagemagick problem: height-and-width not deterministic when I crop an png file. centguy Linux - Software 2 09-09-2009 05:31 AM
extract part of a line with sed or awk alirezan1 Linux - Newbie 2 10-01-2008 09:44 PM
Reduce width and height of gdmgreeter and Xfce desktop lothario Linux - Software 5 06-02-2006 02:27 PM
Laptop Screen Height and Width blueStatic Linux - Hardware 1 09-03-2003 12:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration