Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting FFMPEG errror on certain files. #878

Closed
mrzoops opened this issue Apr 30, 2018 · 19 comments
Closed

Getting FFMPEG errror on certain files. #878

mrzoops opened this issue Apr 30, 2018 · 19 comments

Comments

@mrzoops
Copy link

mrzoops commented Apr 30, 2018

Getting the error below only on certain files. Then it auto deletes the file.

2018-04-29 22:29:09 - uTorrentPostProcess - INFO - uTorrent post processing started.
2018-04-29 22:29:09 - readSettings - INFO - C:\Python27\python.exe
2018-04-29 22:29:09 - uTorrentPostProcess - INFO - Performing conversion
2018-04-29 22:29:09 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Reading video stream.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Video codec detected: h264.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Profile: High.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Reading audio streams.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Audio detected for stream #1: eac3 [eng].
2018-04-29 22:29:09 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-04-29 22:29:09 - mkvtomp4 - INFO - Reading subtitle streams.
2018-04-29 22:29:09 - mkvtomp4 - INFO - Subtitle detected for stream #2: subrip [eng].
2018-04-29 22:29:09 - mkvtomp4 - INFO - Subtitle detected for stream #3: subrip [eng].
2018-04-29 22:29:09 - mkvtomp4 - INFO - Starting conversion.
2018-04-29 22:29:09 - mkvtomp4 - ERROR - Error converting file, FFMPEG error.
Traceback (most recent call last):
  File "C:\Users\Plexbox\Desktop\sickbeard_mp4_automator-master\mkvtomp4.py", line 732, in convert
    for timecode in conv:
  File "C:\Users\Plexbox\Desktop\sickbeard_mp4_automator-master\converter\__init__.py", line 243, in convert
    timeout=timeout, preopts=preopts, postopts=postopts):
  File "C:\Users\Plexbox\Desktop\sickbeard_mp4_automator-master\converter\ffmpeg.py", line 551, in convert
    total_output, pid=p.pid)
FFMpegConvertError: <FFMpegConvertError error="Exited with code 1", pid=18576, cmd="C:\ffmpeg\ffmpeg.exe -hwaccel dxva2 -i C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv -vcodec copy -map 0:0 -c:a:0 aac -map 0:1 -disposition:a:0 default -ac:a:0 6 -b:a:0 1536k -metadata:s:a:0 language=eng -strict experimental -f mp4 -threads auto -y C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mp4">
2018-04-29 22:29:09 - mkvtomp4 - ERROR - C:\ffmpeg\ffmpeg.exe -hwaccel dxva2 -i C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv -vcodec copy -map 0:0 -c:a:0 aac -map 0:1 -disposition:a:0 default -ac:a:0 6 -b:a:0 1536k -metadata:s:a:0 language=eng -strict experimental -f mp4 -threads auto -y C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mp4
2018-04-29 22:29:09 - mkvtomp4 - ERROR - ffmpeg version N-90615-g53688b62ca Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 13.100 / 56. 13.100
  libavcodec     58. 17.100 / 58. 17.100
  libavformat    58. 10.100 / 58. 10.100
  libavdevice    58.  2.100 / 58.  2.100
  libavfilter     7. 14.100 /  7. 14.100
  libswscale      5.  0.102 /  5.  0.102
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.1 : 5.1
Input #0, matroska,webm, from 'C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv':
  Metadata:
    encoder         : libebml v1.3.5 + libmatroska v1.4.8
    creation_time   : 2018-04-30T02:07:53.000000Z
  Duration: 00:30:58.86, start: 0.000000, bitrate: 8423 kb/s
    Chapter #0:0: start 0.000000, end 1.418000
    Metadata:
      title           : Ratings
    Chapter #0:1: start 1.418000, end 38.418000
    Metadata:
      title           : Previously On
    Chapter #0:2: start 38.418000, end 43.418000
    Metadata:
      title           : Studio Logo
    Chapter #0:3: start 43.418000, end 146.418000
    Metadata:
      title           : Interim CEO
    Chapter #0:4: start 146.418000, end 157.418000
    Metadata:
      title           : Title Sequence
    Chapter #0:5: start 157.418000, end 612.418000
    Metadata:
      title           : Scene 1
    Chapter #0:6: start 612.418000, end 1256.418000
    Metadata:
      title           : Scene 1
    Chapter #0:7: start 1256.418000, end 1747.418000
    Metadata:
      title           : Scene 1
    Chapter #0:8: start 1747.418000, end 1817.418000
    Metadata:
      title           : End Credits
    Chapter #0:9: start 1817.418000, end 1858.858000
    Metadata:
      title           : Next On
    Stream #0:0: Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Metadata:
      BPS             : 7780760
      BPS-eng         : 7780760
      DURATION        : 00:30:58.858000000
      DURATION-eng    : 00:30:58.858000000
      NUMBER_OF_FRAMES: 44568
      NUMBER_OF_FRAMES-eng: 44568
      NUMBER_OF_BYTES : 1807916019
      NUMBER_OF_BYTES-eng: 1807916019
      _STATISTICS_WRITING_APP: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2018-04-30 02:07:53
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-30 02:07:53
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:1(eng): Audio: eac3, 48000 Hz, 5.1, fltp (default)
    Metadata:
      BPS             : 640000
      BPS-eng         : 640000
      DURATION        : 00:30:58.784000000
      DURATION-eng    : 00:30:58.784000000
      NUMBER_OF_FRAMES: 58087
      NUMBER_OF_FRAMES-eng: 58087
      NUMBER_OF_BYTES : 148702720
      NUMBER_OF_BYTES-eng: 148702720
      _STATISTICS_WRITING_APP: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2018-04-30 02:07:53
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-30 02:07:53
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:2(eng): Subtitle: subrip
    Metadata:
      BPS             : 101
      BPS-eng         : 101
      DURATION        : 00:30:52.459000000
      DURATION-eng    : 00:30:52.459000000
      NUMBER_OF_FRAMES: 738
      NUMBER_OF_FRAMES-eng: 738
      NUMBER_OF_BYTES : 23446
      NUMBER_OF_BYTES-eng: 23446
      _STATISTICS_WRITING_APP: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2018-04-30 02:07:53
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-30 02:07:53
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:3(eng): Subtitle: subrip
    Metadata:
      title           : SDH
      BPS             : 105
      BPS-eng         : 105
      DURATION        : 00:30:52.459000000
      DURATION-eng    : 00:30:52.459000000
      NUMBER_OF_FRAMES: 779
      NUMBER_OF_FRAMES-eng: 779
      NUMBER_OF_BYTES : 24413
      NUMBER_OF_BYTES-eng: 24413
      _STATISTICS_WRITING_APP: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v19.0.0 ('Brave Captain') 64-bit
      _STATISTICS_WRITING_DATE_UTC: 2018-04-30 02:07:53
      _STATISTICS_WRITING_DATE_UTC-eng: 2018-04-30 02:07:53
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mp4: No such file or directory

2018-04-29 22:29:09 - uTorrentPostProcess - ERROR - Error converting file C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv.
Traceback (most recent call last):
  File "C:\Users\Plexbox\Desktop\sickbeard_mp4_automator-master\uTorrentPostProcess.py", line 144, in <module>
    ignore.append(output['output'])
TypeError: 'bool' object has no attribute '__getitem__'
2018-04-29 22:29:09 - uTorrentPostProcess - INFO - Passing C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert directory to Sonarr.
2018-04-29 22:29:09 - autoprocess.sonarr - INFO - Sonarr notifier started.
2018-04-29 22:29:09 - autoprocess.sonarr - INFO - Requesting Sonarr to scan directory 'C:\Users\Plexbox\Downloads\sonarr\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]\Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert'.
2018-04-29 22:29:10 - autoprocess.sonarr - INFO - Sonarr response: queued.
@mrzoops
Copy link
Author

mrzoops commented May 3, 2018

If I then go back in and run Manual.py against the file, it works normally. Any ideas on why the failure?

@mdhiggins
Copy link
Owner

This is a strange error, its saying 'no such file or directory' with regards to the output file. That's the FFMPEG error
You're only copying streams so there's no reason that it should be failing
I'm wondering if perhaps its a permissions error and its unable to write to the new file

Did it actually create that subdirectory Silicon.Valley.S05E06.Artificial.Emotional.Intelligence.1080p.AMZN.WEBRip.DDP5.1.x264-NTb[rarbg]-convert

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Not sure because the cmd window pops up turn almost instantly closes and deletes all trace of the file and folder if there was one.

@mdhiggins
Copy link
Owner

Try replacing your uTorrentPostProcess.py file with this content and see if it works

import os
import re
import sys
import shutil
from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr
from readSettings import ReadSettings
from mkvtomp4 import MkvtoMp4
import logging
from logging.config import fileConfig

fileConfig(os.path.join(os.path.dirname(sys.argv[0]), 'logging.ini'), defaults={'logfilename': os.path.join(os.path.dirname(sys.argv[0]), 'info.log').replace("\\", "/")})
log = logging.getLogger("uTorrentPostProcess")

log.info("uTorrent post processing started.")

# Args: %L %T %D %K %F %I Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash


def _authToken(session=None, host=None, username=None, password=None):
    auth = None
    if not session:
        session = requests.Session()
    response = session.get(host + "gui/token.html", auth=(username, password), verify=False, timeout=30)
    if response.status_code == 200:
        auth = re.search("<div.*?>(\S+)<\/div>", response.text).group(1)
    else:
        log.error("Authentication Failed - Status Code " + response.status_code + ".")

    return auth, session


def _sendRequest(session, host='http://localhost:8080/', username=None, password=None, params=None, files=None, fnct=None):
    try:
        response = session.post(host + "gui/", auth=(username, password), params=params, files=files, timeout=30)
    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e:
        log.exception("Problem sending command " + fnct + " - " + str(e) + ".")
        return False

    if response.status_code == 200:
        log.debug("Request sent successfully - %s." % fnct)
        return True

    log.error("Problem sending command " + fnct + ", return code = " + str(response.status_code) + ".")
    return False

if len(sys.argv) < 6:
    log.error("Not enough command line parameters present, are you launching this from uTorrent?")
    log.error("#Args: %L %T %D %K %F %I %N Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash, Name")
    log.error("Length was %s" % str(len(sys.argv)))
    log.error(str(sys.argv[1:]))
    sys.exit()

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")
path = str(sys.argv[3])
label = sys.argv[1].lower()
categories = [settings.uTorrent['cp'], settings.uTorrent['sb'], settings.uTorrent['sonarr'], settings.uTorrent['radarr'], settings.uTorrent['sr'], settings.uTorrent['bypass']]
torrent_hash = sys.argv[6]
try:
    name = sys.argv[7]
except:
    name = sys.argv[6]

log.debug("Path: %s." % path)
log.debug("Label: %s." % label)
log.debug("Categories: %s." % categories)
log.debug("Torrent hash: %s." % torrent_hash)
log.debug("Torrent name: %s." % name)

if label not in categories:
    log.error("No valid label detected.")
    sys.exit()

if len(categories) != len(set(categories)):
    log.error("Duplicate category detected. Category names must be unique.")
    sys.exit()

# Import requests
try:
    import requests
except ImportError:
    log.exception("Python module REQUESTS is required. Install with 'pip install requests' then try again.")
    sys.exit()

try:
    web_ui = settings.uTorrentWebUI
    log.debug("WebUI is true.")
except:
    log.debug("WebUI is false.")
    web_ui = False

delete_dir = False

# Run a uTorrent action before conversion.
if web_ui:
    session = requests.Session()
    if session:
        auth, session = _authToken(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword)
        if auth and settings.uTorrentActionBefore:
            params = {'token': auth, 'action': settings.uTorrentActionBefore, 'hash': torrent_hash}
            _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Before Function")
            log.debug("Sending action %s to uTorrent" % settings.uTorrentActionBefore)

if settings.uTorrent['convert']:
    # Check for custom uTorrent output_dir
    if settings.uTorrent['output_dir']:
        settings.output_dir = settings.uTorrent['output_dir']
        log.debug("Overriding output_dir to %s." % settings.uTorrent['output_dir'])

    # Perform conversion.
    log.info("Performing conversion")
    settings.delete = False
    if not settings.output_dir:
        suffix = "-convert"
        # name = name[:260-len(suffix)]
        settings.output_dir = os.path.join(path, ("%s%s" % (name, suffix)))
        if os.name == 'nt':
            settings.output_dir = '\\\\?\\' + settings.output_dir
        if not os.path.exists(settings.output_dir):
            os.mkdir(settings.output_dir)
        delete_dir = settings.output_dir

    converter = MkvtoMp4(settings)

    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        if MkvtoMp4(settings).validSource(inputfile):
            log.info("Processing file %s." % inputfile)
            try:
                output = converter.process(inputfile, reportProgress=True)
            except:
                log.exception("Error converting file %s." % inputfile)
        else:
            log.debug("Ignoring file %s." % inputfile)
    else:
        log.debug("Processing multiple files.")
        ignore = []
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                if MkvtoMp4(settings).validSource(inputfile) and inputfile not in ignore:
                    log.info("Processing file %s." % inputfile)
                    try:
                        output = converter.process(inputfile)
                        if output is not False:
                            ignore.append(output['output'])
                        else:
                            log.error("Converting file failed %s." % inputfile)
                    except:
                        log.exception("Error converting file %s." % inputfile)
                else:
                    log.debug("Ignoring file %s." % inputfile)

    path = converter.output_dir
else:
    suffix = "-copy"
    # name = name[:260-len(suffix)]
    newpath = os.path.join(path, ("%s%s" % (name, suffix)))
    if len(newpath) > 260 and os.name == 'nt':
        settings.output_dir = '\\\\?\\' + settings.output_dir
    if not os.path.exists(newpath):
        os.mkdir(newpath)
        log.debug("Creating temporary directory %s" % newpath)
    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        shutil.copy(inputfile, newpath)
        log.debug("Copying %s to %s" % (inputfile, newpath))
    else:
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                shutil.copy(inputfile, newpath)
                log.debug("Copying %s to %s" % (inputfile, newpath))
    path = newpath
    delete_dir = newpath

if label == categories[0]:
    log.info("Passing %s directory to Couch Potato." % path)
    autoProcessMovie.process(path, settings)
elif label == categories[1]:
    log.info("Passing %s directory to Sickbeard." % path)
    autoProcessTV.processEpisode(path, settings)
elif label == categories[2]:
    log.info("Passing %s directory to Sonarr." % path)
    sonarr.processEpisode(path, settings)
elif label == categories[3]:
    log.info("Passing %s directory to Radarr." % path)
    radarr.processMovie(path, settings)
elif label == categories[4]:
    log.info("Passing %s directory to Sickrage." % path)
    autoProcessTVSR.processEpisode(path, settings)
elif label == categories[5]:
    log.info("Bypassing any further processing as per category.")

# Run a uTorrent action after conversion.
if web_ui:
    if session and auth and settings.uTorrentActionAfter:
        params = {'token': auth, 'action': settings.uTorrentActionAfter, 'hash': torrent_hash}
        _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "After Function")
        log.debug("Sending action %s to uTorrent" % settings.uTorrentActionAfter)

if delete_dir:
    if os.path.exists(delete_dir):
        try:
            os.rmdir(delete_dir)
            log.debug("Successfully removed tempoary directory %s." % delete_dir)
        except:
            log.exception("Unable to delete temporary directory")

sys.exit()

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Yep! Changing that made the file auto process now. What was it?

@mdhiggins
Copy link
Owner

I think it was failing due to an old 260 character limit in file paths that's built into Windows, I had a check in there that would attempt to avoid that but when doing the counter didn't include the filename in the full path
Removed the check and implemented the fix across the board instead which appears to have worked

Can you post the successful log files so I can review them and I'll merge these changes into the project

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Yes here is the log. But it didn't import the created file. The convert directory is there with the MP4, but it never pulled from sonarr into plex.

2018-05-20 12:24:55 - uTorrentPostProcess - INFO - uTorrent post processing started.
2018-05-20 12:24:55 - readSettings - INFO - C:\Python27\python.exe
2018-05-20 12:24:55 - uTorrentPostProcess - INFO - Performing conversion
2018-05-20 12:24:55 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mkv.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Reading video stream.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Video codec detected: h264.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Profile: High.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Reading audio streams.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Audio detected for stream #1: aac [und].
2018-05-20 12:24:59 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-05-20 12:24:59 - mkvtomp4 - INFO - Reading subtitle streams.
2018-05-20 12:24:59 - mkvtomp4 - INFO - Starting conversion.
2018-05-20 12:25:23 - mkvtomp4 - INFO - \?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4 created.
2018-05-20 12:25:23 - uTorrentPostProcess - INFO - Passing \?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert directory to Sonarr.
2018-05-20 12:25:23 - autoprocess.sonarr - INFO - Sonarr notifier started.
2018-05-20 12:25:23 - autoprocess.sonarr - INFO - Requesting Sonarr to scan directory '\?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'.
2018-05-20 12:25:24 - autoprocess.sonarr - INFO - Sonarr response: queued.
2018-05-20 12:25:24 - uTorrentPostProcess - ERROR - Unable to delete temporary directory
Traceback (most recent call last):
File "C:\Users\Plexbox\Desktop\Utilities\sickbeard_mp4_automator-master\uTorrentPostProcess.py", line 204, in
os.rmdir(delete_dir)
WindowsError: [Error 145] The directory is not empty: '\\?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'

@mdhiggins
Copy link
Owner

Hm looks like Sonarr doesn't like those path flags

Try this version instead

import os
import re
import sys
import shutil
from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr
from readSettings import ReadSettings
from mkvtomp4 import MkvtoMp4
import logging
from logging.config import fileConfig

fileConfig(os.path.join(os.path.dirname(sys.argv[0]), 'logging.ini'), defaults={'logfilename': os.path.join(os.path.dirname(sys.argv[0]), 'info.log').replace("\\", "/")})
log = logging.getLogger("uTorrentPostProcess")

log.info("uTorrent post processing started.")

# Args: %L %T %D %K %F %I Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash


def _authToken(session=None, host=None, username=None, password=None):
    auth = None
    if not session:
        session = requests.Session()
    response = session.get(host + "gui/token.html", auth=(username, password), verify=False, timeout=30)
    if response.status_code == 200:
        auth = re.search("<div.*?>(\S+)<\/div>", response.text).group(1)
    else:
        log.error("Authentication Failed - Status Code " + response.status_code + ".")

    return auth, session


def _sendRequest(session, host='http://localhost:8080/', username=None, password=None, params=None, files=None, fnct=None):
    try:
        response = session.post(host + "gui/", auth=(username, password), params=params, files=files, timeout=30)
    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e:
        log.exception("Problem sending command " + fnct + " - " + str(e) + ".")
        return False

    if response.status_code == 200:
        log.debug("Request sent successfully - %s." % fnct)
        return True

    log.error("Problem sending command " + fnct + ", return code = " + str(response.status_code) + ".")
    return False

if len(sys.argv) < 6:
    log.error("Not enough command line parameters present, are you launching this from uTorrent?")
    log.error("#Args: %L %T %D %K %F %I %N Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash, Name")
    log.error("Length was %s" % str(len(sys.argv)))
    log.error(str(sys.argv[1:]))
    sys.exit()

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")
path = str(sys.argv[3])
label = sys.argv[1].lower()
categories = [settings.uTorrent['cp'], settings.uTorrent['sb'], settings.uTorrent['sonarr'], settings.uTorrent['radarr'], settings.uTorrent['sr'], settings.uTorrent['bypass']]
torrent_hash = sys.argv[6]
try:
    name = sys.argv[7]
except:
    name = sys.argv[6]

log.debug("Path: %s." % path)
log.debug("Label: %s." % label)
log.debug("Categories: %s." % categories)
log.debug("Torrent hash: %s." % torrent_hash)
log.debug("Torrent name: %s." % name)

if label not in categories:
    log.error("No valid label detected.")
    sys.exit()

if len(categories) != len(set(categories)):
    log.error("Duplicate category detected. Category names must be unique.")
    sys.exit()

# Import requests
try:
    import requests
except ImportError:
    log.exception("Python module REQUESTS is required. Install with 'pip install requests' then try again.")
    sys.exit()

try:
    web_ui = settings.uTorrentWebUI
    log.debug("WebUI is true.")
except:
    log.debug("WebUI is false.")
    web_ui = False

delete_dir = False

# Run a uTorrent action before conversion.
if web_ui:
    session = requests.Session()
    if session:
        auth, session = _authToken(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword)
        if auth and settings.uTorrentActionBefore:
            params = {'token': auth, 'action': settings.uTorrentActionBefore, 'hash': torrent_hash}
            _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Before Function")
            log.debug("Sending action %s to uTorrent" % settings.uTorrentActionBefore)

if settings.uTorrent['convert']:
    # Check for custom uTorrent output_dir
    if settings.uTorrent['output_dir']:
        settings.output_dir = settings.uTorrent['output_dir']
        log.debug("Overriding output_dir to %s." % settings.uTorrent['output_dir'])

    # Perform conversion.
    log.info("Performing conversion")
    settings.delete = False
    if not settings.output_dir:
        suffix = "-convert"
        # name = name[:260-len(suffix)]
        settings.output_dir = os.path.join(path, ("%s%s" % (name, suffix)))
        if os.name == 'nt':
            settings.output_dir = '\\\\?\\' + settings.output_dir
        if not os.path.exists(settings.output_dir):
            os.mkdir(settings.output_dir)
        delete_dir = settings.output_dir

    converter = MkvtoMp4(settings)

    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        if MkvtoMp4(settings).validSource(inputfile):
            log.info("Processing file %s." % inputfile)
            try:
                output = converter.process(inputfile, reportProgress=True)
            except:
                log.exception("Error converting file %s." % inputfile)
        else:
            log.debug("Ignoring file %s." % inputfile)
    else:
        log.debug("Processing multiple files.")
        ignore = []
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                if MkvtoMp4(settings).validSource(inputfile) and inputfile not in ignore:
                    log.info("Processing file %s." % inputfile)
                    try:
                        output = converter.process(inputfile)
                        if output is not False:
                            ignore.append(output['output'])
                        else:
                            log.error("Converting file failed %s." % inputfile)
                    except:
                        log.exception("Error converting file %s." % inputfile)
                else:
                    log.debug("Ignoring file %s." % inputfile)

    path = converter.output_dir
else:
    suffix = "-copy"
    # name = name[:260-len(suffix)]
    newpath = os.path.join(path, ("%s%s" % (name, suffix)))
    if os.name == 'nt':
        newpath = '\\\\?\\' + newpath
    if not os.path.exists(newpath):
        os.mkdir(newpath)
        log.debug("Creating temporary directory %s" % newpath)
    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        shutil.copy(inputfile, newpath)
        log.debug("Copying %s to %s" % (inputfile, newpath))
    else:
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                shutil.copy(inputfile, newpath)
                log.debug("Copying %s to %s" % (inputfile, newpath))
    path = newpath
    delete_dir = newpath

if path.startswith('\\\\?\\'):
    path = path[4:]

if label == categories[0]:
    log.info("Passing %s directory to Couch Potato." % path)
    autoProcessMovie.process(path, settings)
elif label == categories[1]:
    log.info("Passing %s directory to Sickbeard." % path)
    autoProcessTV.processEpisode(path, settings)
elif label == categories[2]:
    log.info("Passing %s directory to Sonarr." % path)
    sonarr.processEpisode(path, settings)
elif label == categories[3]:
    log.info("Passing %s directory to Radarr." % path)
    radarr.processMovie(path, settings)
elif label == categories[4]:
    log.info("Passing %s directory to Sickrage." % path)
    autoProcessTVSR.processEpisode(path, settings)
elif label == categories[5]:
    log.info("Bypassing any further processing as per category.")

# Run a uTorrent action after conversion.
if web_ui:
    if session and auth and settings.uTorrentActionAfter:
        params = {'token': auth, 'action': settings.uTorrentActionAfter, 'hash': torrent_hash}
        _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "After Function")
        log.debug("Sending action %s to uTorrent" % settings.uTorrentActionAfter)

if delete_dir:
    if os.path.exists(delete_dir):
        try:
            os.rmdir(delete_dir)
            log.debug("Successfully removed tempoary directory %s." % delete_dir)
        except:
            log.exception("Unable to delete temporary directory")

sys.exit()

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Damn. Still not importing after convert:

2018-05-20 12:48:46 - uTorrentPostProcess - INFO - uTorrent post processing started.
2018-05-20 12:48:46 - readSettings - INFO - C:\Python27\python.exe
2018-05-20 12:48:46 - uTorrentPostProcess - INFO - Performing conversion
2018-05-20 12:48:46 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mkv.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Reading video stream.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Video codec detected: h264.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Profile: High.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Reading audio streams.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Audio detected for stream #1: aac [und].
2018-05-20 12:48:46 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-05-20 12:48:46 - mkvtomp4 - INFO - Reading subtitle streams.
2018-05-20 12:48:46 - mkvtomp4 - INFO - Starting conversion.
2018-05-20 12:49:06 - mkvtomp4 - INFO - \?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4 created.
2018-05-20 12:49:06 - uTorrentPostProcess - INFO - Passing C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert directory to Sonarr.
2018-05-20 12:49:06 - autoprocess.sonarr - INFO - Sonarr notifier started.
2018-05-20 12:49:06 - autoprocess.sonarr - INFO - Requesting Sonarr to scan directory 'C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'.
2018-05-20 12:49:07 - autoprocess.sonarr - INFO - Sonarr response: queued.
2018-05-20 12:49:07 - uTorrentPostProcess - ERROR - Unable to delete temporary directory
Traceback (most recent call last):
File "C:\Users\Plexbox\Desktop\Utilities\sickbeard_mp4_automator-master\uTorrentPostProcess.py", line 207, in
os.rmdir(delete_dir)
WindowsError: [Error 145] The directory is not empty: '\\?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'

@mdhiggins
Copy link
Owner

Alright looking through Sonarr's issues it looks like Sonarr also bugs out with >260 characters on Windows and they are looking in to fixing this on V3 but this has not been implemented yet

I can do some subtle changes that should at least help keep that count under 260 in the mean time by prevented the convert directory from duplicating the long torrent name. Hopefully this won't interfere with Sonarrs ability to identify the file

Try this version

import os
import re
import sys
import shutil
from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr
from readSettings import ReadSettings
from mkvtomp4 import MkvtoMp4
import logging
from logging.config import fileConfig

fileConfig(os.path.join(os.path.dirname(sys.argv[0]), 'logging.ini'), defaults={'logfilename': os.path.join(os.path.dirname(sys.argv[0]), 'info.log').replace("\\", "/")})
log = logging.getLogger("uTorrentPostProcess")

log.info("uTorrent post processing started.")

# Args: %L %T %D %K %F %I Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash


def _authToken(session=None, host=None, username=None, password=None):
    auth = None
    if not session:
        session = requests.Session()
    response = session.get(host + "gui/token.html", auth=(username, password), verify=False, timeout=30)
    if response.status_code == 200:
        auth = re.search("<div.*?>(\S+)<\/div>", response.text).group(1)
    else:
        log.error("Authentication Failed - Status Code " + response.status_code + ".")

    return auth, session


def _sendRequest(session, host='http://localhost:8080/', username=None, password=None, params=None, files=None, fnct=None):
    try:
        response = session.post(host + "gui/", auth=(username, password), params=params, files=files, timeout=30)
    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e:
        log.exception("Problem sending command " + fnct + " - " + str(e) + ".")
        return False

    if response.status_code == 200:
        log.debug("Request sent successfully - %s." % fnct)
        return True

    log.error("Problem sending command " + fnct + ", return code = " + str(response.status_code) + ".")
    return False

if len(sys.argv) < 6:
    log.error("Not enough command line parameters present, are you launching this from uTorrent?")
    log.error("#Args: %L %T %D %K %F %I %N Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash, Name")
    log.error("Length was %s" % str(len(sys.argv)))
    log.error(str(sys.argv[1:]))
    sys.exit()

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")
path = str(sys.argv[3])
label = sys.argv[1].lower()
categories = [settings.uTorrent['cp'], settings.uTorrent['sb'], settings.uTorrent['sonarr'], settings.uTorrent['radarr'], settings.uTorrent['sr'], settings.uTorrent['bypass']]
torrent_hash = sys.argv[6]
try:
    name = sys.argv[7]
except:
    name = sys.argv[6]

log.debug("Path: %s." % path)
log.debug("Label: %s." % label)
log.debug("Categories: %s." % categories)
log.debug("Torrent hash: %s." % torrent_hash)
log.debug("Torrent name: %s." % name)

if label not in categories:
    log.error("No valid label detected.")
    sys.exit()

if len(categories) != len(set(categories)):
    log.error("Duplicate category detected. Category names must be unique.")
    sys.exit()

# Import requests
try:
    import requests
except ImportError:
    log.exception("Python module REQUESTS is required. Install with 'pip install requests' then try again.")
    sys.exit()

try:
    web_ui = settings.uTorrentWebUI
    log.debug("WebUI is true.")
except:
    log.debug("WebUI is false.")
    web_ui = False

delete_dir = False

# Run a uTorrent action before conversion.
if web_ui:
    session = requests.Session()
    if session:
        auth, session = _authToken(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword)
        if auth and settings.uTorrentActionBefore:
            params = {'token': auth, 'action': settings.uTorrentActionBefore, 'hash': torrent_hash}
            _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Before Function")
            log.debug("Sending action %s to uTorrent" % settings.uTorrentActionBefore)

if settings.uTorrent['convert']:
    # Check for custom uTorrent output_dir
    if settings.uTorrent['output_dir']:
        settings.output_dir = settings.uTorrent['output_dir']
        log.debug("Overriding output_dir to %s." % settings.uTorrent['output_dir'])

    # Perform conversion.
    log.info("Performing conversion")
    settings.delete = False
    if not settings.output_dir:
        suffix = "convert"
        # name = name[:260-len(suffix)]
        settings.output_dir = os.path.join(path, suffix)
        if os.name == 'nt':
            settings.output_dir = '\\\\?\\' + settings.output_dir
        if not os.path.exists(settings.output_dir):
            os.mkdir(settings.output_dir)
        delete_dir = settings.output_dir

    converter = MkvtoMp4(settings)

    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        if MkvtoMp4(settings).validSource(inputfile):
            log.info("Processing file %s." % inputfile)
            try:
                output = converter.process(inputfile, reportProgress=True)
            except:
                log.exception("Error converting file %s." % inputfile)
        else:
            log.debug("Ignoring file %s." % inputfile)
    else:
        log.debug("Processing multiple files.")
        ignore = []
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                if MkvtoMp4(settings).validSource(inputfile) and inputfile not in ignore:
                    log.info("Processing file %s." % inputfile)
                    try:
                        output = converter.process(inputfile)
                        if output is not False:
                            ignore.append(output['output'])
                        else:
                            log.error("Converting file failed %s." % inputfile)
                    except:
                        log.exception("Error converting file %s." % inputfile)
                else:
                    log.debug("Ignoring file %s." % inputfile)

    path = converter.output_dir
else:
    suffix = "copy"
    # name = name[:260-len(suffix)]
    newpath = os.path.join(path, suffix)
    if os.name == 'nt':
        newpath = '\\\\?\\' + newpath
    if not os.path.exists(newpath):
        os.mkdir(newpath)
        log.debug("Creating temporary directory %s" % newpath)
    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        shutil.copy(inputfile, newpath)
        log.debug("Copying %s to %s" % (inputfile, newpath))
    else:
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                shutil.copy(inputfile, newpath)
                log.debug("Copying %s to %s" % (inputfile, newpath))
    path = newpath
    delete_dir = newpath

if path.startswith('\\\\?\\'):
    path = path[4:]

if label == categories[0]:
    log.info("Passing %s directory to Couch Potato." % path)
    autoProcessMovie.process(path, settings)
elif label == categories[1]:
    log.info("Passing %s directory to Sickbeard." % path)
    autoProcessTV.processEpisode(path, settings)
elif label == categories[2]:
    log.info("Passing %s directory to Sonarr." % path)
    sonarr.processEpisode(path, settings)
elif label == categories[3]:
    log.info("Passing %s directory to Radarr." % path)
    radarr.processMovie(path, settings)
elif label == categories[4]:
    log.info("Passing %s directory to Sickrage." % path)
    autoProcessTVSR.processEpisode(path, settings)
elif label == categories[5]:
    log.info("Bypassing any further processing as per category.")

# Run a uTorrent action after conversion.
if web_ui:
    if session and auth and settings.uTorrentActionAfter:
        params = {'token': auth, 'action': settings.uTorrentActionAfter, 'hash': torrent_hash}
        _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "After Function")
        log.debug("Sending action %s to uTorrent" % settings.uTorrentActionAfter)

if delete_dir:
    if os.path.exists(delete_dir):
        try:
            os.rmdir(delete_dir)
            log.debug("Successfully removed tempoary directory %s." % delete_dir)
        except:
            log.exception("Unable to delete temporary directory")

sys.exit()

Reference:
Sonarr/Sonarr#636

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

OK that would make sense. Still happening with updating py file though.

2018-05-20 13:09:30 - uTorrentPostProcess - INFO - uTorrent post processing started.
2018-05-20 13:09:30 - readSettings - INFO - C:\Python27\python.exe
2018-05-20 13:09:30 - uTorrentPostProcess - INFO - Performing conversion
2018-05-20 13:09:30 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mkv.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Reading video stream.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Video codec detected: h264.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Profile: High.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Reading audio streams.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Audio detected for stream #1: aac [und].
2018-05-20 13:09:30 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-05-20 13:09:30 - mkvtomp4 - INFO - Reading subtitle streams.
2018-05-20 13:09:30 - mkvtomp4 - INFO - Starting conversion.
2018-05-20 13:09:51 - mkvtomp4 - INFO - \?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4 created.
2018-05-20 13:09:51 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Reading video stream.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Video codec detected: h264.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Profile: High.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Reading audio streams.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Audio detected for stream #1: aac [eng].
2018-05-20 13:09:51 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-05-20 13:09:51 - mkvtomp4 - INFO - Reading subtitle streams.
2018-05-20 13:09:51 - mkvtomp4 - INFO - Starting conversion.
2018-05-20 13:09:51 - mkvtomp4 - INFO - \?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4 created.
2018-05-20 13:09:51 - uTorrentPostProcess - INFO - Passing C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert directory to Sonarr.
2018-05-20 13:09:51 - autoprocess.sonarr - INFO - Sonarr notifier started.
2018-05-20 13:09:51 - autoprocess.sonarr - INFO - Requesting Sonarr to scan directory 'C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert'.
2018-05-20 13:09:52 - autoprocess.sonarr - INFO - Sonarr response: queued.
2018-05-20 13:09:52 - uTorrentPostProcess - ERROR - Unable to delete temporary directory
Traceback (most recent call last):
File "C:\Users\Plexbox\Desktop\Utilities\sickbeard_mp4_automator-master\uTorrentPostProcess.py", line 413, in
os.rmdir(delete_dir)
WindowsError: [Error 145] The directory is not empty: '\\?\C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert'

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

And the path to the file: C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4

Only looks to be 184 characters

I also tried with a previously working file, and it imports correctly.

@mdhiggins
Copy link
Owner

Yeah it should be working now
Is Sonarr showing anything in its logs to indicate why its not moving the file?

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Not sure the location of sonarr logs that would be relevant. Nothing I can find showing anything related to the import. Nothing even related to the successful import of the other file so I might not be looking in the right spot.

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

OK here's something I just noticed. Right at the start of the conversion, I check the mp4 file while its being created and the file size is over 1gb (original file is 1.3gb). After conversion, the original is deleted. I check the newly created mp4 file and its only 35kb.

@mdhiggins
Copy link
Owner

mdhiggins commented May 20, 2018

I'm just gonna remove the weird directory syntaxing for windows and go for a method that results in shorter filenames
Try this

import os
import re
import sys
import shutil
from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr
from readSettings import ReadSettings
from mkvtomp4 import MkvtoMp4
import logging
from logging.config import fileConfig

fileConfig(os.path.join(os.path.dirname(sys.argv[0]), 'logging.ini'), defaults={'logfilename': os.path.join(os.path.dirname(sys.argv[0]), 'info.log').replace("\\", "/")})
log = logging.getLogger("uTorrentPostProcess")

log.info("uTorrent post processing started.")

# Args: %L %T %D %K %F %I Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash


def _authToken(session=None, host=None, username=None, password=None):
    auth = None
    if not session:
        session = requests.Session()
    response = session.get(host + "gui/token.html", auth=(username, password), verify=False, timeout=30)
    if response.status_code == 200:
        auth = re.search("<div.*?>(\S+)<\/div>", response.text).group(1)
    else:
        log.error("Authentication Failed - Status Code " + response.status_code + ".")

    return auth, session


def _sendRequest(session, host='http://localhost:8080/', username=None, password=None, params=None, files=None, fnct=None):
    try:
        response = session.post(host + "gui/", auth=(username, password), params=params, files=files, timeout=30)
    except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError), e:
        log.exception("Problem sending command " + fnct + " - " + str(e) + ".")
        return False

    if response.status_code == 200:
        log.debug("Request sent successfully - %s." % fnct)
        return True

    log.error("Problem sending command " + fnct + ", return code = " + str(response.status_code) + ".")
    return False

if len(sys.argv) < 6:
    log.error("Not enough command line parameters present, are you launching this from uTorrent?")
    log.error("#Args: %L %T %D %K %F %I %N Label, Tracker, Directory, single|multi, NameofFile(if single), InfoHash, Name")
    log.error("Length was %s" % str(len(sys.argv)))
    log.error(str(sys.argv[1:]))
    sys.exit()

settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini")
path = str(sys.argv[3])
label = sys.argv[1].lower()
categories = [settings.uTorrent['cp'], settings.uTorrent['sb'], settings.uTorrent['sonarr'], settings.uTorrent['radarr'], settings.uTorrent['sr'], settings.uTorrent['bypass']]
torrent_hash = sys.argv[6]
try:
    name = sys.argv[7]
except:
    name = sys.argv[6]

log.debug("Path: %s." % path)
log.debug("Label: %s." % label)
log.debug("Categories: %s." % categories)
log.debug("Torrent hash: %s." % torrent_hash)
log.debug("Torrent name: %s." % name)

if label not in categories:
    log.error("No valid label detected.")
    sys.exit()

if len(categories) != len(set(categories)):
    log.error("Duplicate category detected. Category names must be unique.")
    sys.exit()

# Import requests
try:
    import requests
except ImportError:
    log.exception("Python module REQUESTS is required. Install with 'pip install requests' then try again.")
    sys.exit()

try:
    web_ui = settings.uTorrentWebUI
    log.debug("WebUI is true.")
except:
    log.debug("WebUI is false.")
    web_ui = False

delete_dir = False

# Run a uTorrent action before conversion.
if web_ui:
    session = requests.Session()
    if session:
        auth, session = _authToken(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword)
        if auth and settings.uTorrentActionBefore:
            params = {'token': auth, 'action': settings.uTorrentActionBefore, 'hash': torrent_hash}
            _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "Before Function")
            log.debug("Sending action %s to uTorrent" % settings.uTorrentActionBefore)

if settings.uTorrent['convert']:
    # Check for custom uTorrent output_dir
    if settings.uTorrent['output_dir']:
        settings.output_dir = settings.uTorrent['output_dir']
        log.debug("Overriding output_dir to %s." % settings.uTorrent['output_dir'])

    # Perform conversion.
    log.info("Performing conversion")
    settings.delete = False
    if not settings.output_dir:
        suffix = "convert"
        if str(sys.argv[4]) == 'single':
            log.info("Single File Torrent")
            settings.output_dir = os.path.join(path, ("%s-%s" % (name, suffix)))
        else:
            log.info("Multi File Torrent")
            settings.output_dir = os.path.abspath(os.path.join(path, '..', ("%s-%s" % (name, suffix))))
        if not os.path.exists(settings.output_dir):
            os.mkdir(settings.output_dir)
        delete_dir = settings.output_dir

    converter = MkvtoMp4(settings)

    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        if MkvtoMp4(settings).validSource(inputfile):
            log.info("Processing file %s." % inputfile)
            try:
                output = converter.process(inputfile, reportProgress=True)
            except:
                log.exception("Error converting file %s." % inputfile)
        else:
            log.debug("Ignoring file %s." % inputfile)
    else:
        log.debug("Processing multiple files.")
        ignore = []
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                if MkvtoMp4(settings).validSource(inputfile) and inputfile not in ignore:
                    log.info("Processing file %s." % inputfile)
                    try:
                        output = converter.process(inputfile)
                        if output is not False:
                            ignore.append(output['output'])
                        else:
                            log.error("Converting file failed %s." % inputfile)
                    except:
                        log.exception("Error converting file %s." % inputfile)
                else:
                    log.debug("Ignoring file %s." % inputfile)

    path = converter.output_dir
else:
    suffix = "copy"
    # name = name[:260-len(suffix)]
    if str(sys.argv[4]) == 'single':
        log.info("Single File Torrent")
        newpath = os.path.join(path, ("%s-%s" % (name, suffix)))
    else:
        log.info("Multi File Torrent")
        newpath = os.path.abspath(os.path.join(path, '..', ("%s-%s" % (name, suffix))))
    if not os.path.exists(newpath):
        os.mkdir(newpath)
        log.debug("Creating temporary directory %s" % newpath)
    if str(sys.argv[4]) == 'single':
        inputfile = os.path.join(path, str(sys.argv[5]))
        shutil.copy(inputfile, newpath)
        log.debug("Copying %s to %s" % (inputfile, newpath))
    else:
        for r, d, f in os.walk(path):
            for files in f:
                inputfile = os.path.join(r, files)
                shutil.copy(inputfile, newpath)
                log.debug("Copying %s to %s" % (inputfile, newpath))
    path = newpath
    delete_dir = newpath

if label == categories[0]:
    log.info("Passing %s directory to Couch Potato." % path)
    autoProcessMovie.process(path, settings)
elif label == categories[1]:
    log.info("Passing %s directory to Sickbeard." % path)
    autoProcessTV.processEpisode(path, settings)
elif label == categories[2]:
    log.info("Passing %s directory to Sonarr." % path)
    sonarr.processEpisode(path, settings)
elif label == categories[3]:
    log.info("Passing %s directory to Radarr." % path)
    radarr.processMovie(path, settings)
elif label == categories[4]:
    log.info("Passing %s directory to Sickrage." % path)
    autoProcessTVSR.processEpisode(path, settings)
elif label == categories[5]:
    log.info("Bypassing any further processing as per category.")

# Run a uTorrent action after conversion.
if web_ui:
    if session and auth and settings.uTorrentActionAfter:
        params = {'token': auth, 'action': settings.uTorrentActionAfter, 'hash': torrent_hash}
        _sendRequest(session, settings.uTorrentHost, settings.uTorrentUsername, settings.uTorrentPassword, params, None, "After Function")
        log.debug("Sending action %s to uTorrent" % settings.uTorrentActionAfter)

if delete_dir:
    if os.path.exists(delete_dir):
        try:
            os.rmdir(delete_dir)
            log.debug("Successfully removed tempoary directory %s." % delete_dir)
        except:
            log.exception("Unable to delete temporary directory")

sys.exit()

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

Boom. That was it. Converted and imported perfectly. Also it finally deleted both directories completely which it never used to do. Thank you so much!

@mrzoops
Copy link
Author

mrzoops commented May 20, 2018

2018-05-20 14:17:29 - uTorrentPostProcess - INFO - uTorrent post processing started.
2018-05-20 14:17:29 - readSettings - INFO - C:\Python27\python.exe
2018-05-20 14:17:29 - uTorrentPostProcess - INFO - Performing conversion
2018-05-20 14:17:29 - uTorrentPostProcess - INFO - Multi File Torrent
2018-05-20 14:17:29 - uTorrentPostProcess - INFO - Processing file C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mkv.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Reading video stream.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Video codec detected: h264.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Pix Fmt: yuv420p.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Profile: High.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Reading audio streams.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Audio detected for stream #1: aac [und].
2018-05-20 14:17:29 - mkvtomp4 - INFO - Creating audio stream 0 from source stream 1.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Audio Track is number 1 setting disposition to default
2018-05-20 14:17:29 - mkvtomp4 - INFO - Reading subtitle streams.
2018-05-20 14:17:29 - mkvtomp4 - INFO - Starting conversion.
2018-05-20 14:17:50 - mkvtomp4 - INFO - C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert\stephen.colbert.2018.05.18.benedict.cumberbatch.1080p.web.x264-tbs.mp4 created.
2018-05-20 14:17:50 - uTorrentPostProcess - INFO - Passing C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert directory to Sonarr.
2018-05-20 14:17:50 - autoprocess.sonarr - INFO - Sonarr notifier started.
2018-05-20 14:17:50 - autoprocess.sonarr - INFO - Requesting Sonarr to scan directory 'C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'.
2018-05-20 14:17:51 - autoprocess.sonarr - INFO - Sonarr response: queued.
2018-05-20 14:17:51 - uTorrentPostProcess - ERROR - Unable to delete temporary directory
Traceback (most recent call last):
File "C:\Users\Plexbox\Desktop\Utilities\sickbeard_mp4_automator-master\uTorrentPostProcess.py", line 417, in
os.rmdir(delete_dir)
WindowsError: [Error 145] The directory is not empty: 'C:\Users\Plexbox\Downloads\sonarr\Stephen.Colbert.2018.05.18.Benedict.Cumberbatch.1080p.WEB.x264-TBS[rarbg]-convert'

@mdhiggins
Copy link
Owner

Sweet, I'll merge these commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants