2

Is it possible to pass a text file as stdinput to ffmpegfrom which it can read all the parameters present in the text file?

Something like this:

ffmpeg < foo.txt

Where foo.txt contains

-i < file.mkv -c:v libx265 -preset medium "Encoded_file.mkv"

Reason: Hide the files/parameters from programs such as htop

  • 2
    If you have the parameters in a file, you can launch the command in bash with ffmpeg $(cat foo.txt). Alternatively, add the ffmpeg command to the file and make it a script, eg ./foo.sh (or in Windows .\foo.cmd). – AFH Nov 1 '17 at 10:18
2

Some options can be given to ffmpeg using preset files, check the documentation here. This way, only the name of the preset file should be visible in htop. Some hints on preset files can be found in this answer. From what I have gathered from the documentation, you will not be able to hide the codec parameter, just options given to that codec. More hands-on examples can be found here.

Judging from your example you don't give any of these options that could be written to a preset file.


The input file can be passed via standard input by specifying the special file - and piping the file content to ffmpeg like so:

cat kooky_720p.mp4 | ffmpeg -i - -c:v libx265 -preset medium out.mkv

The htop display of this command looks like this: htop extract 1


The output file can, similarly, be changed to the special file - which will redirect the converted file to standard output. To do this, you have to specify the container format manually, as ffmpeg will not be able to guess it from the file name.

Then you can pipe the converted data to the destination file. This could look like that:

cat kooky_720p.mp4 | ffmpeg -i - -c:v libx265 -preset medium -f matroska - > out.mkv

The htop display of this command looks like this: htop extract 2

I do not know under which circumstances htop would display the file being piped into, but I guess this is handled inside the shell, so it is likely it will also not show up in shells other than zsh.


To also hide the cat input.ext from htop you could use e.g. a small python script like this to read the filename from a text file and copy the content to stdout:

#!/usr/bin/env python
from __future__ import print_function
import shutil
import sys

# Print errors to stderr, since we will pipe to stdout
# For an explanation, see https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

# Check if there is a filename given
if len(sys.argv) < 2:
    eprint("Please specify an input file")
    sys.exit(1)

# Open the file and read the first line
try:
    fl = open(sys.argv[1], "r")
    filename = fl.readline().strip()
    fl.close()
except Exception as e:
    eprint("Could not read filename from " + sys.argv[1])
    eprint(e)
    sys.exit(1)

# Pipe the content from the file to standard output
try:
    with open(filename, "rb") as ffmpegInput:
        if sys.version_info >= (3, 0):
            # Python 3
            shutil.copyfileobj(ffmpegInput, sys.stdout.buffer)
        else:
            # Python 2
            shutil.copyfileobj(ffmpegInput, sys.stdout)
except Exception as e:
    eprint("Could not pipe input file " + filename)
    eprint(e)
    sys.exit(1)

Usage could be like this, whereas filename.txt is a simple text file containing one line of text that has the video file to be read, kooky_720p.mp4 in our example.

./pipe.py filename.txt  | ffmpeg -i - -c:v libx265 -preset medium -f matroska - > out.mkv

The htop display of this command looks like this: htop extract 3


I belive this is the closest you will get to hiding information from htop. Remember though, that it is still possible to get the information which files you are processing by looking at the open file handles e.g. using lsof:

$ pidof python3
... 19013 ...

$ lsof -a -p 19013   
COMMAND   PID   USER   FD   TYPE DEVICE   SIZE/OFF     NODE NAME
python3 19013 oliver  cwd    DIR   8,84       4096 31197648 /home/oliver/Videos/Kooky
python3 19013 oliver  rtd    DIR   8,66       4096        2 /
python3 19013 oliver  txt    REG   8,66       9992  7781733 /usr/bin/python3.6
python3 19013 oliver  mem    REG   8,66      11752  8020773 /usr/lib/python3.6/lib-dynload/grp.cpython-36m-x86_64-linux-gnu.so
python3 19013 oliver  mem    REG   8,66     154344  7743563 /usr/lib/liblzma.so.5.2.3
python3 19013 oliver  mem    REG   8,66      36856  8012780 /usr/lib/python3.6/lib-dynload/_lzma.cpython-36m-x86_64-linux-gnu.so
python3 19013 oliver  mem    REG   8,66      66960  7739636 /usr/lib/libbz2.so.1.0.6
python3 19013 oliver  mem    REG   8,66      21400  8012783 /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so
python3 19013 oliver  mem    REG   8,66      92056  7736853 /usr/lib/libz.so.1.2.11
python3 19013 oliver  mem    REG   8,66      36744  8012786 /usr/lib/python3.6/lib-dynload/zlib.cpython-36m-x86_64-linux-gnu.so
python3 19013 oliver  mem    REG   8,66      21384  8020788 /usr/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so
python3 19013 oliver  mem    REG   8,66    3268160  7787080 /usr/lib/locale/locale-archive
python3 19013 oliver  mem    REG   8,66    1358168  7735428 /usr/lib/libm-2.26.so
python3 19013 oliver  mem    REG   8,66      10048  7735415 /usr/lib/libutil-2.26.so
python3 19013 oliver  mem    REG   8,66      14144  7735429 /usr/lib/libdl-2.26.so
python3 19013 oliver  mem    REG   8,66    3327592  7785916 /usr/lib/libpython3.6m.so.1.0
python3 19013 oliver  mem    REG   8,66    2065840  7735493 /usr/lib/libc-2.26.so
python3 19013 oliver  mem    REG   8,66     145336  7735516 /usr/lib/libpthread-2.26.so
python3 19013 oliver  mem    REG   8,66     176880  7735494 /usr/lib/ld-2.26.so
python3 19013 oliver    0u   CHR  136,1        0t0        4 /dev/pts/1
python3 19013 oliver    1w  FIFO   0,11        0t0  1276096 pipe
python3 19013 oliver    2u   CHR  136,1        0t0        4 /dev/pts/1
python3 19013 oliver    3r   REG   8,84 4363354747 31198177 /home/oliver/Videos/Kooky/kooky_720p.mp4

As you can see, it lists the video file I used as an example here. The output file will show up in the handles of the ffmpeg process.

  • This is beautiful . The third method is the one I tried. Thanks a lot @founderio It'd be great if I could hide the parameters too but I am satisfied with this anyway :) – RuMAN S Nov 1 '17 at 16:04
-1

Simply cat the parameters:

$ ffmpeg $(cat foo.txt)
  • 2
    Thanks.. Yes I can do that but it won't hide the parameters from "htop". I just edited my question with the reason why I want to do this – RuMAN S Nov 1 '17 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.