JakeWharton/mkvdts2ac3

find a way to clean up ffmpeg output and provide progress bar

Closed this issue · 3 comments

Ok, I think I have it! After lots of hours of testing and tweaking and an idea from Rupert Plumridge I have come up with a pretty slick solution. I run ffmpeg in the background outputing to a tmp file and parse the tmp file pulling out just what i want and massaging the data to give a rough sense of progress. A TODO could be to calculate percentage and display that as well.

This is what it looks like when running:

Converting DTS to AC3:
size=   11961kB/119254kB time=218.72 bitrate= 448.0kbits/s    

Here is the whole conversion section for review. It should be a direct cut from the first line and replace up to the last line:

# ------ CONVERSION ------
# Convert DTS to AC3
doprint $"Converting DTS to AC3."
doprint "> ffmpeg -i \"$DTSFILE\" -acodec ac3 -ac 6 -ab 448k \"$AC3FILE\""

dopause
if [ $EXECUTE = 1 ]; then
        color YELLOW; echo $"Converting DTS to AC3:"; color OFF
        nice -n $PRIORITY ffmpeg -i "$DTSFILE" -acodec ac3 -ac 6 -ab 448k "$AC3FILE" 2>/tmp/ffmpeg_output &
        PID=$!
        DTSFILESIZE=$($DUCMD "$DTSFILE" | cut -f1) # Capture DTS filesize for end summary
        let AC3FILESIZE=$DTSFILESIZE/5  #Estimate on AC3 ending filesize based on typical use
        while [ -e /proc/$PID ];do
                LINE=$(tail -1 /tmp/ffmpeg_output|sed "s/kB/kB\/${AC3FILESIZE}kB/g") #Inject size in output
                echo -ne "\r$LINE"   # Provide updates from ffmpeg_output every second
                sleep 1
        done
        checkerror $? $"Converting the DTS file to AC3 failed" 1
        cleanup /tmp/ffmpeg_output

        # If we are keeping the DTS track external copy it back to original folder before deleting
        if [ ! -z $KEEPDTS ]; then
                color YELLOW; echo $"Moving DTS track to MKV directory."; color OFF
                rsync --progress -a "$DTSFILE" "$DEST"
                checkerror $? $"There was an error copying the DTS track to the MKV directory. You can perform
 this manually from \"$DTSFILE\"." 1
        fi
        cleanup $DTSFILE

        timestamp $"DTS track conversion took: "
fi

I might be able to use this same concept to provide a progress bar for Extracting DTS. Need to revisit this again.

Submitted a version with this to dev branch. Please test and ensure no issues

The next step is to turn the ugly output from ffmpeg into a normal Progress: 100% method. This has proven a lot harder that I wanted but with some help from a friend I have a concise way of doing this. The problem is ffmpeg, remember how heinous ffmpeg output is (!@#$%), it requires me to use perl to parse it out as our normal she'll tools require \n as an end of line and ffmpeg is using \r.

QUESTION: Do we have a problem requiring Perl on a system? I don't know of a linux system these days that doesn't have perl installed by default but does OSX have perl installed by default? I would have to have to force people to install perl just for a single line of code; but it sure is nice having a pretty progress %.

Output is cleaned up and progress percentage works as expected. Closing issue.