0

Using ffmpeg I can specify the start time and duration of each segment. For example I wrote

ffmpeg -i "originalvideo.mp4" -ss 00:00 -t 2:34 -vcodec copy -acodec copy "smallervideo1.mp4"

I have a spreadsheet where I have 3 columns; name of smaller video, start time and end time. I could create each command separately for each video but that would take some time given I am splitting the sources files into close to 100 smaller videos.

Is there a method where i can pass the values from the spreadsheet into ffmpeg and output each video with the appropriate name automatically?

1

This can be done with a simple shell script, in this case a Bash script. If you have a whitespace-delimited file as input, which contains the output file name, the start timestamp and the end timestamp, e.g.:

$ cat cuts.txt
foo.mp4 00:00:00 00:00:01
bar.mp4 00:01:20 00:02:00

Then read this with a simple loop and construct your ffmpeg command:

while read -r filename start end; do
  ffmpeg -i "input.mp4" -ss "$start" -to "$end" -c copy "$filename"
done < cuts.txt

This just cuts the bitstream without encoding – -c copy is a shorthand for -vcodec copy -acodec copy (and it copies subtitles as well). You can specify a video encoder (e.g. -c:v libx264) and an audio encoder (e.g., -c:a aac -b:a 192k) to re-encode the video.

A more portable, but basic version with Python 3:

#!/usr/bin/env python3
import subprocess
with open("cuts.txt") as f:
  for line in f.readlines():
    filename, start, end = line.strip().split(' ')
    cmd = ["ffmpeg", "-i", "input.mp4", "-ss", start, "-to", end, "-c", "copy", filename]
    subprocess.run(cmd, stderr=subprocess.STDOUT)

Note: If you run this on Windows, you should add ffmpeg to your PATH, or specify its full path like C:/Program Files/ffmpeg/bin/ffmpeg.exe.

| improve this answer | |
  • Thanks @slhck. I have a spreadsheet but I a assume I can get it into that format easily. I will try this out. – Michael Horwitz Jul 11 '17 at 13:57
  • You can just export it to CSV from Excel, for example, but use a single space as delimiter. Or if you have a comma as delimiter, use while read -r -d, filename start end … to specify another delimiter. (Use .split(',') in Python in that case.) – slhck Jul 11 '17 at 14:19
  • since I use Windows I went with the Python approach. I am stuck and Im not sure what the issue is. My Code is import subprocess with open("cuts3.txt") as f: for line in f.readlines(): filename, start, end = line.strip().split(' ') cmd = 'C:\ffmpeg\bin\ffmpeg.exe -i Recordings\recording6.6.17.mp4 -ss {start} -to {end} -c copy "{filename}"'.format(**locals()) subprocess.run(cmd, shell=True) print ("loop over") The txt file is Greetings.mp4 0:00:00 0:02:35 Introduction.mp4 0:02:35 0:05:10 – Michael Horwitz Jul 12 '17 at 11:30
  • @MichaelHorwitz Comments are not really useful for this kind of troubleshooting… block code formatting doesn't work there. But do you get any error messages? Note that for Windows you should use forward slashes to run the command. – slhck Jul 12 '17 at 12:35
  • See my edits above, maybe this works better for you. – slhck Jul 12 '17 at 12:48

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.