Question

So I am trying to script some video file downloads from my ftp and send them to handbrake for encoding.

I was having trouble with keeping the ftp socket open, but due to an amazing response from a SO user that is solved but:

Now the file that is getting downloaded is not recognized by handbrake. This particular file encoded fine before I implemented the socket solution so its something to do with how the data is being written to the file.

Here is my code:

if ext in validExtensions:
        print("Downloading new file: " + fil)
        downloadFile(fil, newPath)
        print("Download complete. Encoding: " + fil)
        hb(newPath + f)

here is downloadFile function code:

def downloadFile(filename, folder):
    myhost = 'host'
    myuser = 'user'
    passw = 'pass'
    #login
    ftp = FTP(myhost,myuser,passw)
    ftp.set_debuglevel(2)
    sock = ftp.transfercmd('RETR ' + filename)
    def background():
        f = open(folder + filename, 'wb')
        while True:
            block = sock.recv(1024*1024)
            if not block:
                break
            f.write(block)
        sock.close()
        f.close()
    t = threading.Thread(target=background)
    t.start()
    while t.is_alive():
        t.join(60)
        ftp.voidcmd('NOOP')
    ftp.quit()

here is handbrake.py:

def HandbrakeEncode(filepath):
    import subprocess
    import os
    filename = os.path.basename(filepath)
    file, ext = os.path.splitext(filename)
    outputPath = "D:\\Converted Mp4\\" + file + ".mp4"
    args = '-i ' + filepath + ' -o '+ outputPath

    #Popen expects arguments borken up into chunks by spaces
    cmd = ['C:\\Program Files\\Handbrake\\HandbrakeCLI.exe',
           '-i', filepath,
           '-o', outputPath,
           'preset="AppleTV 2"']


    p = subprocess.call(cmd)
    #delete the original
    os.remove(filepath)

and here is the output I get from handrake

Download complete. Encoding: myvid.mp4
[20:52:39] hb_init: starting libhb thread
HandBrake 0.9.9 (2013052900) - MinGW x86_64 - http://handbrake.fr
4 CPUs detected
Opening D:\Downloads\AutoVid\myvid.mp4...
[20:52:39] hb_scan: path=D:\Downloads\Auto\myvid.m
p4, title_index=1
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening D:\Downloads\Auto
\myvid.mp4/BDMV/index.bdmv
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening D:\Downloads\Auto
\myvid.mp4/BDMV/BACKUP/index.bdmv
libbluray/bluray.c:1725: nav_get_title_list(D:\Downloads\Auto\myvid.mp4) failed (0000000001CD6900)
[20:52:39] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 4.1.3
libdvdread: Encrypted DVD support unavailable.
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[20:52:39] dvd: not a dvd - trying as a stream/file instead
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000001CD73C0] moov atom not found
[20:52:39] hb_stream_open: open D:\Downloads\Auto\myvid.mp4 failed
[20:52:39] scan: unrecognized file type
[20:52:39] libhb: scan thread found 0 valid title(s)
No title found.

HandBrake has exited.

UPDATE: Ok I'm closer, I have identified that the original transfer is transferring in BINARY and this is now TRANSFERRING in ASCII. Once I figure out how to set it back to BINARY we should be ok

Was it helpful?

Solution

The solution for me was to send an explicit "TYPE I" Command before performing the transfer

hope this helps someone else new code:

ftp = FTP(myhost,myuser,passw)
    ftp.sendcmd('TYPE I')
    ftp.set_debuglevel(2)
    sock = ftp.transfercmd('RETR ' + filename)
    def background():
        f = open(folder + filename, 'wb')
        while True:
            block = sock.recv(1024*1024)
            if not block:
                break
            f.write(block)
        sock.close()
    t = threading.Thread(target=background)
    t.start()
    while t.is_alive():
        t.join(60)
        ftp.voidcmd('NOOP')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top