How can I test for successful encode in bash script?

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
JamesAB
Posts: 6
Joined: Sat Jun 27, 2015 5:28 am

How can I test for successful encode in bash script?

Post by JamesAB »

I understand that Handbrake CLI may exit with a "0" exit code although that "does NOT indicate a complete or error-free encode" according to the docs.

I'm writing a bash script to batch convert many different types of files into .mp4 format. How should I test the output files after handbrake is complete to make sure a valid .mp4 file was created?

I'm dealing with about 100,000 videos so I really need to automate this and make sure the .mp4 files created by the Handbrake CLI are "playable" before publishing them to the web.

For example, I see some files that were created must not be valid video files because I get "html5: Video not found" errors when trying to view these files with web based html5 players like Flowplayer.

Thanks for your help,
James
mduell
Veteran User
Posts: 8198
Joined: Sat Apr 21, 2007 8:54 pm

Re: How can I test for successful encode in bash script?

Post by mduell »

Define successful.
JamesAB
Posts: 6
Joined: Sat Jun 27, 2015 5:28 am

Re: How can I test for successful encode in bash script?

Post by JamesAB »

Okay. For me "successful" will mean a video that will not show an "html5: Video not found" error when I try to play in online with with web-based players like Flowplayer.

Successful will be a video that can be played by html5 players.
User avatar
s55
HandBrake Team
Posts: 10357
Joined: Sun Dec 24, 2006 1:05 pm

Re: How can I test for successful encode in bash script?

Post by s55 »

Unless flow player provides some way to test, you can't. Even a perfectly valid file may not play if that player has limitations, or bugs etc.
Deleted User 11865

Re: How can I test for successful encode in bash script?

Post by Deleted User 11865 »

JamesAB wrote:I understand that Handbrake CLI may exit with a "0" exit code although that "does NOT indicate a complete or error-free encode" according to the docs.
True. But (ever since Maxym added more return codes, IIRC), any known errors will result in a non-zero exit code. HandBrake can't report errors that went by undetected I'm afraid…
mduell
Veteran User
Posts: 8198
Joined: Sat Apr 21, 2007 8:54 pm

Re: How can I test for successful encode in bash script?

Post by mduell »

JamesAB wrote:Okay. For me "successful" will mean a video that will not show an "html5: Video not found" error when I try to play in online with with web-based players like Flowplayer.

Successful will be a video that can be played by html5 players.
Does Flowplayer have a test suite that can readily be automated? It's a black box to us what limitations it has.

Do any of your other html5 players have a test suite that can readily be automated? Again, black box to us.
JamesAB
Posts: 6
Joined: Sat Jun 27, 2015 5:28 am

Re: How can I test for successful encode in bash script?

Post by JamesAB »

I've tested this on Flowplayer and JW Player (both javasrcipt based media players that get embedded on web pages) and saw the exact same error: "html5: Video not found". The error is slightly cryptic because the source file is correct and found by the players. It's just the something about video itself that the players do not like. Neither player offer much support for encoding advice or testing suites.

I'm new to handbrake so I'm trying to understand some more of the CLI output information.
Here's the full output from one of the files I'm working with:

Code: Select all

[root@phy5sl forum321]# ./HandBrakeCLI -i /home/bbattach4/3/0/1/4/9/5597679.attach -o /home/forum321/public_html/testvideos/out3.mp4 --preset="iPhone & iPod touch" -I -O
[13:12:07] hb_init: starting libhb thread
[13:12:07] thread 7fd24a86a700 started ("libhb")
HandBrake svn7320 (2015062601) - Linux x86_64 - https://handbrake.fr
16 CPUs detected
Opening /home/bbattach4/3/0/1/4/9/5597679.attach...
[13:12:07] CPU: Intel(R) Xeon(R) CPU           E5620  @ 2.40GHz
[13:12:07]  - logical processor count: 16
[13:12:07] OpenCL: library not available
[13:12:07] hb_scan: path=/home/bbattach4/3/0/1/4/9/5597679.attach, title_index=1
udfread ERROR: ECMA 167 Volume Recognition failed
disc.c:251: failed opening UDF image /home/bbattach4/3/0/1/4/9/5597679.attach
disc.c:332: error opening file BDMV/index.bdmv
disc.c:332: error opening file BDMV/BACKUP/index.bdmv
[13:12:07] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 5.0.1
libdvdread: Encrypted DVD support unavailable.
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[13:12:07] dvd: not a dvd - trying as a stream/file instead
Input #0, flv, from '/home/bbattach4/3/0/1/4/9/5597679.attach':
  Metadata:
    canSeekToEnd    : false
    createdby       : FMS 3.5
    creationdate    : Sun May 15 01:47:59 2011
  Duration: 00:09:19.07, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: flv, yuv420p, 640x480, 4.42 fps, 1k tbn
    Stream #0.1: Audio: nellymoser, 8000 Hz, mono, flt
[13:12:07] scan: decoding previews for title 1
[13:12:08] scan: audio 0x1: nellymoser, rate=8000Hz, bitrate=1 Unknown (nellymoser) (1.0 ch)
Scanning title 1 of 1, preview 6, 60.00 %[13:12:08] scan: 10 previews, 640x480, 23.976 fps, autocrop = 0/0/0/0, aspect 4:3, PAR 1:1
[13:12:08] libhb: scan thread found 1 valid title(s)
+ Using preset: iPhone & iPod touch
+ title 1:
  + stream: /home/bbattach4/3/0/1/4/9/5597679.attach
  + duration: 00:09:19
  + size: 640x480, pixel aspect: 1/1, display aspect: 1.33, 23.976 fps
  + autocrop: 0/0/0/0
  + support opencl: no
  + chapters:
    + 1: cells 0->0, 0 blocks, duration 00:09:19
  + audio tracks:
    + 1, Unknown (nellymoser) (1.0 ch) (iso639-2: und)
  + subtitle tracks:
[13:12:08] 1 job(s) to process
[13:12:08] json job:
{
    "Audio": {
        "AudioList": [
            {
                "Bitrate": 160,
                "CompressionLevel": -1.0,
                "DRC": 0.0,
                "DitherMethod": "auto",
                "Encoder": "fdk_aac",
                "Gain": 0.0,
                "Mixdown": "dpl2",
                "NormalizeMixLevel": false,
                "PresetEncoder": "fdk_aac",
                "Samplerate": "auto",
                "Track": 0
            }
        ],
        "CopyMask": [
            "copy:aac",
            "copy:ac3",
            "copy:dts",
            "copy:dtshd",
            "copy:eac3",
            "copy:flac",
            "copy:mp3",
            "copy:truehd"
        ],
        "FallbackEncoder": "ac3"
    },
    "Destination": {
        "ChapterList": [
            {
                "Name": ""
            }
        ],
        "ChapterMarkers": false,
        "File": "/home/forum321/public_html/testvideos/out3.mp4",
        "Mp4Options": {
            "IpodAtom": true,
            "Mp4Optimize": true
        },
        "Mux": "m4v"
    },
    "Filters": {
        "FilterList": [
            {
                "ID": 5,
                "Settings": "2"
            },
            {
                "ID": 10,
                "Settings": "640:480:0:0:0:0"
            }
        ],
        "Grayscale": false
    },
    "Metadata": {},
    "PAR": {
        "Den": 1,
        "Num": 1
    },
    "SequenceID": 0,
    "Source": {
        "Angle": 0,
        "Path": "/home/bbattach4/3/0/1/4/9/5597679.attach",
        "Range": {
            "End": 1,
            "Start": 1,
            "Type": "chapter"
        },
        "Title": 1
    },
    "Subtitle": {
        "Search": {
            "Burn": true,
            "Default": false,
            "Enable": false,
            "Forced": false
        },
        "SubtitleList": []
    },
    "Video": {
        "Bitrate": -1,
        "ColorMatrixCode": 0,
        "Encoder": "x264",
        "HWDecode": false,
        "Level": "3.1",
        "OpenCL": false,
        "Options": "",
        "Preset": "medium",
        "Profile": "high",
        "QSV": {
            "AsyncDepth": 4,
            "Decode": false
        },
        "Quality": 22.0,
        "Tune": "",
        "Turbo": false,
        "TwoPass": false
    }
}
[13:12:08] starting job
[13:12:08] work: sanitizing track 1 mixdown Dolby Pro Logic II to Mono
[13:12:08] work: sanitizing track 1 bitrate 160 to 48 Kbps
[13:12:08] sync: expecting 13404 video frames
[13:12:08] job configuration:
[13:12:08]  * source
[13:12:08]    + /home/bbattach4/3/0/1/4/9/5597679.attach
[13:12:08]    + title 1, chapter(s) 1 to 1
[13:12:08]    + container: flv
[13:12:08]  * destination
[13:12:08]    + /home/forum321/public_html/testvideos/out3.mp4
[13:12:08]    + container: MPEG-4 (libavformat)
[13:12:08]      + optimized for HTTP streaming (fast start)
[13:12:08]      + compatibility atom for iPod 5G
[13:12:08]  * video track
[13:12:08]    + decoder: flv
[13:12:08]    + filters
[13:12:08]      + Framerate Shaper (2)
[13:12:08]        + frame rate: 23.976 fps -> peak rate limited to 23.976 fps
[13:12:08]      + Crop and Scale (640:480:0:0:0:0)
[13:12:08]        + source: 640 * 480, crop (0/0/0/0): 640 * 480, scale: 640 * 480
[13:12:08]    + Output geometry
[13:12:08]      + storage dimensions: 640 x 480
[13:12:08]      + pixel aspect ratio: 1 : 1
[13:12:08]      + display dimensions: 640 x 480
[13:12:08]    + encoder: H.264 (libx264)
[13:12:08]      + preset:  medium
[13:12:08]      + profile: high
[13:12:08]      + level:   3.1
[13:12:08]      + quality: 22.00 (RF)
[13:12:08]  * audio track 1
[13:12:08]    + decoder: Unknown (nellymoser) (1.0 ch) (track 1, id 0x1)
[13:12:08]      + samplerate: 8000 Hz
[13:12:08]    + mixdown: Mono
[13:12:08]    + dither: triangular
[13:12:08]    + encoder: AAC (libfdk_aac)
[13:12:08]      + bitrate: 48 kbps, samplerate: 8000 Hz
[13:12:08] encx264: min-keyint: 24, keyint: 240
[13:12:08] encx264: encoding at constant RF 22.000000
[13:12:08] encx264: unparsed options: level=3.1:vbv-maxrate=17500:vbv-bufsize=17500
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[13:12:08] reader: first SCR 0 id 0x0 DTS 0
x264 [info]: profile High, level 3.1
Encoding: task 1 of 1, 9.89 % (32.22 fps, avg 31.97 fps, ETA 00h06m18s)[13:12:50] sync: adding 92 ms of silence to audio 0x1  start 43530840, next 43522560
Encoding: task 1 of 1, 10.59 % (32.46 fps, avg 32.00 fps, ETA 00h06m15s)[13:12:53] reader: done. 1 scr changes
Encoding: task 1 of 1, 11.00 % (32.61 fps, avg 32.03 fps, ETA 00h06m12s)[13:12:57] work: average encoding speed for job is 32.032513 fps
[13:12:57] sync: got 1475 frames, 13404 expected
[13:12:58] render: 1475 frames output, 0 dropped and 0 duped for CFR/PFR
[13:12:58] render: lost time: 0 (0 frames)
[13:12:58] render: gained time: 0 (0 frames) (0 not accounted for)
[13:12:58] flv-decoder done: 1476 frames, 0 decoder errors, 0 drops
x264 [info]: frame I:38    Avg QP:15.04  size: 22521
x264 [info]: frame P:1099  Avg QP:17.78  size: 21405
x264 [info]: frame B:338   Avg QP:19.19  size: 18891
x264 [info]: consecutive B-frames: 56.4% 37.6%  4.7%  1.4%
x264 [info]: mb I  I16..4: 17.6% 78.0%  4.4%
x264 [info]: mb P  I16..4:  7.7% 66.4%  3.2%  P16..4:  8.8%  7.5%  5.3%  0.0%  0.0%    skip: 1.0%
x264 [info]: mb B  I16..4:  1.7% 34.6%  2.3%  B16..8: 16.7% 15.7%  9.3%  direct:17.2%  skip: 2.5%  L0:48.2% L1:31.8% BI:19.9%
x264 [info]: 8x8 transform intra:86.1% inter:85.7%
x264 [info]: coded y,uvDC,uvAC intra: 82.5% 94.6% 56.2% inter: 63.5% 79.0% 50.4%
x264 [info]: i16 v,h,dc,p: 32% 21% 18% 29%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 26% 30%  4%  3%  3%  3%  3%  5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 29% 11%  5%  5%  4%  6%  4%  6%
x264 [info]: i8c dc,h,v,p: 42% 27% 23%  7%
x264 [info]: Weighted P-Frames: Y:7.5% UV:5.4%
x264 [info]: ref P L0: 47.1% 16.4% 22.6% 13.5%  0.4%
x264 [info]: ref B L0: 71.9% 26.6%  1.5%
x264 [info]: ref B L1: 95.0%  5.0%
x264 [info]: kb/s:440.21
Encoding: task 1 of 1, 11.00 % (32.61 fps, avg 32.03 fps, ETA 00h06m12s)[13:12:58] nellymoser-decoder done: 0 frames, 0 decoder errors, 0 drops
[13:12:58] mux: track 0, 1475 frames, 30764019 bytes, 440.59 kbps, fifo 512
[13:12:58] mux: track 1, 4367 frames, 3353856 bytes, 48.03 kbps, fifo 4096
[13:12:58] libhb: work result = 0

Encode done!

HandBrake has exited.
Something that caught my eye here was the line: sync: got 1475 frames, 13404 expected
mduell
Veteran User
Posts: 8198
Joined: Sat Apr 21, 2007 8:54 pm

Re: How can I test for successful encode in bash script?

Post by mduell »

Does the output video play in VLC?

Does the input video play in VLC?
JamesAB
Posts: 6
Joined: Sat Jun 27, 2015 5:28 am

Re: How can I test for successful encode in bash script?

Post by JamesAB »

I can play both the original and the output with VLC,

Does Handbrake really have exit codes other than "0" and "1"? If so, where can I read about them?

Thanks,
James
User avatar
JohnAStebbins
HandBrake Team
Posts: 5723
Joined: Sat Feb 09, 2008 7:21 pm

Re: How can I test for successful encode in bash script?

Post by JohnAStebbins »

JamesAB wrote:Does Handbrake really have exit codes other than "0" and "1"? If so, where can I read about them?
https://trac.handbrake.fr/browser/trunk ... mon.h#L109
tlindgren
Bright Spark User
Posts: 260
Joined: Sun May 03, 2009 2:14 pm

Re: How can I test for successful encode in bash script?

Post by tlindgren »

Rodeo wrote:True. But (ever since Maxym added more return codes, IIRC), any known errors will result in a non-zero exit code. HandBrake can't report errors that went by undetected I'm afraid…
I guess if one wanted to catch even more failures one way for full encodes might be to compare the run-time for input and output files via https://mediaarea.net/en/MediaInfo on both, though it has to allow for minor differences (say 1% or a few seconds) or it might give a lot of false positives (MediaInfo isn't perfect).

This only works on some types of media, I guess another approach might be to compare the estimated run-time printed during scan phase of the encode log with the result from MediaInfo on the output file, this will also have (different) problems but could work with a some file-types the first approach wouldn't work on.

But fundamentally there's no way to be sure the encode is fine without a human watching it closely, one obvious example is DVD/Blu-ray where they've put many nearly identical copies on the media, only one which plays scenes in the correct order. No amount of machine checking is likely to catch "selected the wrong Title" and I suspect there's other inherently undetectable cases.

So it may well not be worth putting in the effort to try checking this beyond the error code unless the script is going to be used to encode very large number of files.
JamesAB
Posts: 6
Joined: Sat Jun 27, 2015 5:28 am

Re: How can I test for successful encode in bash script?

Post by JamesAB »

Thanks for all your advice.
I am tasked with encoding about 100,000 videos of all sorts of types. I need to automate this as much as possible.
Deleted User 11865

Re: How can I test for successful encode in bash script?

Post by Deleted User 11865 »

j45 recently added code that also returns non-zero in case of read error (before, we treated any read error as if we had reached the end-of-file).
nlucchesi
Posts: 12
Joined: Tue Mar 17, 2015 12:00 am

Re: How can I test for successful encode in bash script?

Post by nlucchesi »

JohnAStebbins wrote:
JamesAB wrote:Does Handbrake really have exit codes other than "0" and "1"? If so, where can I read about them?
https://trac.handbrake.fr/browser/trunk ... mon.h#L109
This link (https://trac.handbrake.fr/browser/trunk ... mon.h#L109) no longer works.

Where can I find a list of return codes and their meanings?

Thank you.

- nello
Post Reply