Question

As I have a low-end computer running Linux I often need to re-encode HD videos, lowering the quality to be able to watch them in my machine. A typical case is when I download several episodes of a series, I can't convert them all at once, and I need to start re-encoding halfway through the series.

I typically use this line to convert a single episode to a lower quality:

avconv -i anime_episode_01.mkv -map 0 -c copy -c:v libx264 -crf 31 anime_01.mkv

If I were to batch-convert them at once I would use something like:

for i in *.mkv;do avconv -i "$i" -map 0 -c copy -c:v libx264 -crf 31 "encoded/$i";done

Where encoded is a subdirectory.

But what if I need to start re-encoding at, say, episode 5?

I have no idea.

Was it helpful?

Solution

There are probably lots of ways to do this, here are a couple of options.

Option 1: Use seq

Use seq to generate a sequence, loop over this and encode.

A sequence from 5 to 15:

seq 5 15

If you need to format the numbers, e.g. to get a 0 prefix for one digit numbers, you can use the -f switch, which takes a printf style formatting argument of a float/double.

seq -f %02.0f 5 15

This can be used in a loop, e.g. something like this:

for i in $(seq -f %02.0f 5 15); do
    filename="anime_episode${i}.mkv"
    echo "Encoding episode $i: $filename"
    avconv -i "$filename" -map 0 -c copy -c:v libx264 -crf 31 "encoded/$filename"
done

Option 2: Check whether encoded file exists

Do pretty much the same as you do in your current loop, but only perform encoding if the encoded file does not already exist.

for i in *.mkv; do
    if [ ! -f encoded/$i ]; then
        echo "Encoding file: $i"
        avconv -i "$i" -map 0 -c copy -c:v libx264 -crf 31 "encoded/$i"
    else
        echo "Skipped file: $i"
    fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top