LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   ffmpeg mass convert & merge video files from various formats (https://www.linuxquestions.org/questions/linux-software-2/ffmpeg-mass-convert-and-merge-video-files-from-various-formats-4175443491/)

lpallard 12-30-2012 11:49 AM

ffmpeg mass convert & merge video files from various formats
 
Hi all! I am pretty sure this wont be a tough one for the real ffmpeg Gurus but I have achieved minor success so far in converting videos with ffmpeg.

Basically, I have a folder with all kind of video files (.flv, .wmv, .mpeg .mp4...) you got the idea.. These files all have different resolutions (some are 640x480, others are 320x240, etc..) and I also assume they also have all kind of different codecs, etc..

I take that some were encoded in windows, some in Mac, some in Linux and with all kind of methods, applications etc..

In other words, its a mess. I'd like to mass (batch) convert all files to the exact same format (container, resolution, codecs, etc) for later on merge (concatenate) all of them into a single file.

A script would be nice. I'd dump all files in a folder, run the script and bingo, I get a big file and Im done.

So far I have used this command within the folder where all files are stored:
Quote:

$IFS=$'\n' ;for f in `ls *.wmv`; do FILE=$(basename $f .wmv) ;ffmpeg -i $FILE.wmv -target ntsc-dv $FILE.mpg; done
The problem is that it processes only wmv files, and I am not sure it will work flawlessly when I will play the output file (I am not knowledgeable enough in video processing to determine that). Also, the converted files are like 300%+ of the original file size.. I believe this is the -target ntsc-dv which is responsible of that..

I am pretty sure that there must be a script someone already worked on. I couldnt find anything like that.. Right now I am in some kind of a rush so I am looking for a quick fix instead of learning ffmpeg..

If someone can point me to something I'd be very grateful, otherwise I will keep searching and trying.

Thanks!

lpallard 12-30-2012 11:57 AM

I *may* have found something. Trying as we "speak".... Ill post the results when done, but I am confident it will work. If so, KUDOS to the original writer!

To be continued !

schneidz 12-30-2012 01:48 PM

something like this mite work:
Code:

/usr/bin/mencoder *.* -o /dev/null -vc mpeg12 -vf pp=al:c,softskip,unsharp=l5x5:.25:c5x5:.25,harddup -channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=192 -a52drc 1 -ovc xvid -xvidencopts pass=1:turbo:me_quality=6:vhq=4:threads=2:max_bframes=2:bvhq=1:nopacked:quant_type=h263:noqpel:nogmc:trellis:nointerlacing:chroma_me:chroma_opt:hq_ac:nolumi_mask:rc_reaction_delay_factor=0:rc_averaging_period=100:closed_gop:autoaspect -passlogfile out.log

/usr/bin/mencoder *.* -o moo-vee.avi -ffourcc XVID -vc mpeg12 -vf pp=al:c,softskip,unsharp=l5x5:.25:c5x5:.25,harddup -channels 6 -oac lavc -lavcopts acodec=ac3:abitrate=192 -a52drc 1 -ovc xvid -xvidencopts pass=2:bitrate=1500:me_quality=6:vhq=4:threads=2:max_bframes=2:bvhq=1:nopacked:quant_type=h263:noqpel:nogmc:trellis:nointerlacing:chroma_me:chroma_opt:hq_ac:nolumi_mask:rc_reaction_delay_factor=0:rc_averaging_period=100:closed_gop:autoaspect -passlogfile out.log

sleep 2

/usr/bin/mkvmerge  --title moo-vee --track-name 0:moo-vee --language 1:en --track-name 1:"AC3 5.1" moo-vee.avi  -o moo-vee.mkv

you didnt say what format/codec/resolution you wanted so i made many assumptions you will need to correct.

lpallard 12-30-2012 06:03 PM

Wow these are LONNNNNG commands!! No chance I would have ever been able to formulate those..

The first command throws

Code:

-xvidencopts is not an MEncoder option
I indeed forgot to indicate which options I was searching for. MKV with 640x480 would be more than fine as most video clips are already crappy.. Other options as standard as possible (audio, framerate, etc). The end file will play on XBMC.

Can you modify these commands? Better, are you aware of some kind of wizard that could help me build such a command without spending countless hours trying different options until it works..?

schneidz 12-30-2012 06:10 PM

Quote:

Originally Posted by lpallard (Post 4859727)
Wow these are LONNNNNG commands!! No chance I would have ever been able to formulate those..

The first command throws

Code:

-xvidencopts is not an MEncoder option

maybe you dont have xvid installed. if you run
Code:

xbmc@XBMCLive:~$ mencoder -ovc help
it will show you what options you have.
Quote:

Originally Posted by lpallard (Post 4859727)
Can you modify these commands? Better, are you aware of some kind of wizard that could help me build such a command without spending countless hours trying different options until it works..?

the above was the output script i saved from xvidenc or h264enc.

lpallard 12-30-2012 07:32 PM

You were right. My machine did not have xvid as a codec. However, I have tried on a different machine that has everything installed, the command (the first one you provided) starts well but after several minutes (10 or so..) it throws away:

Code:

==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 22050 Hz, 1 ch, s16le, 16.0 kbit/4.54% (ratio: 2003->44100)
Selected audio codec: [ffwmav2] afm: ffmpeg (DivX audio v2 (FFmpeg))
==========================================================================
Opening video filter: [expand osd=1]
Expand: -1 x -1, -1 ; -1, osd: 1, aspect: 0.000000, round: 1
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
Selected video codec: [ffwmv1] vfm: ffmpeg (FFmpeg WMV1/WMV7)
==========================================================================
Movie-Aspect is undefined - no prescaling applied.

New video file has different resolution or colorspace than the previous one.
FATAL: Cannot initialize video driver.
Movie-Aspect is undefined - no prescaling applied.

New video file has different resolution or colorspace than the previous one.
FATAL: Cannot initialize video driver.

Exiting...

Cannot initialize video driver? What does that mean in "layman's terms"??

David the H. 12-30-2012 11:02 PM

Could you please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

And please edit your previous posts so that it doesn't make the window side-scroll. Thanks!


Speaking of which, the command in the OP has a few weaknesses. I'm going to start by breaking it into a multi-line format, for readability:

Code:

$IFS=$'\n'
for f in `ls *.wmv`; do
        FILE=$(basename $f .wmv)
        ffmpeg -i $FILE.wmv -target ntsc-dv $FILE.mpg
done


1) The first problem is Don't Read Lines With For! While setting IFS to newline does work to avoid the main problem with it, it's more of a cludgy work-around than a true fix.


2) Useless Use of ls *. It does absolutely nothing here except waste an extra process.

Both 1 and 2 can be replaced more safely with a simple globbing pattern.


3) QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Again, just like in #1, re-setting IFS works around it, but it's safer to just do it correctly.


4) While not actually a mistake, basename can be replaced with a simple parameter substitution.

There's also a kind of unusual and unnecessary code flow in the above, removing the extension globally, then re-adding it.

5) Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them. You should also generally try to use variable names that clearly relate to their function in the script (but that don't conflict with existing command names, like "file").


Thus the corrected version should more properly look more like this:

Code:

for fname in *.wmv; do

        ffmpeg -i "$fname" -target ntsc-dv "${fname%.*}.mpg"

done

#or in a single line:
for fname in *.wmv; do ; ffmpeg -i "$fname" -target ntsc-dv "${fname%.*}.mpg" ; done


schneidz 12-31-2012 12:03 AM

Quote:

Originally Posted by lpallard (Post 4859752)
You were right. My machine did not have xvid as a codec. However, I have tried on a different machine that has everything installed, the command (the first one you provided) starts well but after several minutes (10 or so..) it throws away:



Cannot initialize video driver? What does that mean in "layman's terms"??

seems like mencoder is expecting all the files to have the same resolution. changing all the files to the same resolution first mite work (not sure why mencoder cant do that in one throw).
maybe ffmpeg would be more efficient.


All times are GMT -5. The time now is 07:14 PM.