Python Forum
Get an FFMpeg pass to subprocess.PIPE to treat list as text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get an FFMpeg pass to subprocess.PIPE to treat list as text file?
#1
The following FFMpeg command will import a text file to concatenate/join said file together:
ffmpeg -f concat -safe 0 -i "list_Of_Videos.txt" -c copy "output.mp4"

Alternatively, the following bash terminal will pipe traversed files in a directory:
ffmpeg -f concat -i <( for f in *.wav; do echo "file '$(pwd)/$f'"; done ) output.wav

My question regards the first statement. Can I substitute list_Of_Videos.txt with a python array list variable, instead?

FFMpegs requies that 2 streams be specified, one for stdout, the other for stderr:
process = subprocess.Popen(shlex.split(cmd),
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT)
I don't want to utilize the Python built-in FFMpeg wrapper, but instead looking to work with the separate/stand-alone FFMpeg program.
Reply
#2
Try this,work when i test.
import subprocess

list_of_files = ['v1.mp4', 'v2.mp4', 'v3.mp4']
# Prepare the concat list
concat_list = '\n'.join([f"file '{file}'" for file in list_of_files])

# FFmpeg command with protocol whitelist
cmd = [
    'ffmpeg',
    '-f', 'concat',
    '-safe', '0',
    '-protocol_whitelist', 'file,pipe',
    '-i', '-',
    '-c', 'copy',
    'output.mp4'
]
process = subprocess.Popen(
    cmd,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    universal_newlines=True  # Ensure text mode for stdin
)

# Send the concat list to FFmpeg and get the output
output, _ = process.communicate(input=concat_list)
print(output)
By adding -protocol_whitelist file,pipe to FFmpeg command,
allow FFmpeg to use the necessary protocols to read your concat list from stdin and access the video files for concatenation.
haihal likes this post
Reply
#3
(Nov-21-2024, 08:12 AM)snippsat Wrote: Try this,work when i test.
...
...
process = subprocess.Popen(
    cmd,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    universal_newlines=True  # Ensure text mode for stdin
)
# Send the concat list to FFmpeg and get the output
output, _ = process.communicate(input=concat_list)
print(output)
...
...
Thank you very much. It worked. Just had to make minor changes.
Newer versions of ffmpeg require that the list of files, if one wishes to use pipe, be preceded with:
file file:'input_1.mp4' instead of file 'input_1.mp4'

Also for the statement:
output, _ = process.communicate(input='\n'.join(concat_list))
I had to prepend the list variable with '\n'.join method, as input= demanded an str variable, Expected type 'str', got 'List[str]' instead

Thank you again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  subprocess check_output text cut off Axel_Erfurt 5 815 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  FFMPEG Leondagreatest 0 483 Feb-12-2025, 09:03 PM
Last Post: Leondagreatest
  pass arguments from bat file to pyhon script from application absolut 2 1,100 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Pipe traceback to a C program Alexandros 0 710 Oct-22-2024, 12:32 PM
Last Post: Alexandros
  Absolute paths in subprocess - file not found kittyticker 4 3,038 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,904 Nov-09-2023, 10:56 AM
Last Post: mg24
  How to pass encrypted pass to pyodbc script tester_V 0 1,744 Jul-27-2023, 12:40 AM
Last Post: tester_V
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,038 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Convert Excel file into csv with Pipe symbol.. mg24 4 2,289 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 10,039 Aug-11-2022, 08:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020