<p>You can try using <a href="https://ffmpeg.org/download.html" rel="nofollow noreferrer">ffprobe</a> <a href="https://www.ffmpeg.org/ffprobe-all.html#select_002c-aselect" rel="nofollow noreferrer">scene detection</a> tool to find the black frame time, then use <a href="https://ffmpeg.org/download.html" rel="nofollow noreferrer">ffmpeg</a> to <a href="https://trac.ffmpeg.org/wiki/Seeking" rel="nofollow noreferrer">seek</a> and trim.</p>

<p>I'm not very good at scripting, but here is something you can try. It is very important that the black frame be only a single frame for this to work! Also, ffprobe's scene tool is very picky about file paths, so make sure there are no spaces or special characters in the filename or file path of any of the videos you need to edit.</p>

<p>Copy and paste the following code into a bash or command script file. I'll walk through each step to explain what's happening after.</p>

<pre><code>#!/bin/bash

echo "Enter Source Folder"

read input

cd "$input"

mkdir -p "$input"/output

dir="$input"/output

for video in *; do

if [[ -f "$video" ]]; then

filename="${video%.*}"
extension="${video##*.}"

mkdir "$dir"/"$filename"

dest="$dir"/"$filename"

ffprobe -show_frames -of compact=p=0 -f lavfi "movie="$video",select=gt(scene\,0.9)" -show_entries frame=best_effort_timestamp_time &gt; tmp.txt

sed -e 's/.*best_effort_timestamp_time=\(.*\)|tag:lavfi.scene_score.*/\1/' tmp.txt &gt; chapter.txt
rm tmp.txt

file=chapter.txt
while read line
do
    time+=($line)   
done &lt;"$file"

ffmpeg -i "$video" -ss "${time[0]}" -to "${time[1]}" "$dest"/middle-"$filename"."$extension"

rm chapter.txt

fi

done
</code></pre>

<hr>

<p><strong>Breakdown</strong></p>

<p>Determine the folder that contains the videos you want to trim and set it as the working directory.</p>

<pre><code>#!/bin/bash
echo "Enter Source Folder"
read input
cd "$input"
</code></pre>

<p>Make output directory</p>

<pre><code>mkdir -p "$input"/output
dir="$input"/output
</code></pre>

<p>For each file in the folder do this:</p>

<ol>
<li>make sure it's a file and not a folder </li>
<li>save the filename and extension as a variable</li>
<li><p>create a new output directory for each video</p>

<pre><code>for video in *; do
if [[ -f "$video" ]]; then
filename="${video%.*}"
extension="${video##*.}"
mkdir "$dir"/"$filename"
dest="$dir"/"$filename"
</code></pre></li>
<li><p>Find any frames that are 9/10 different than the frame before and save the time to a txt file.</p>

<pre><code>ffprobe -show_frames -of compact=p=0 -f lavfi "movie="$video",select=gt(scene\,0.9)" -show_entries frame=best_effort_timestamp_time &gt; tmp.txt
</code></pre></li>
<li><p>Clean up and replace text file.</p>

<pre><code>sed -e 's/.*best_effort_timestamp_time=\(.*\)|tag:lavfi.scene_score.*/\1/' tmp.txt &gt; chapter.txt
rm tmp.txt
</code></pre></li>
<li><p>Create array variable from the text file times.</p>

<pre><code>file=chapter.txt
while read line
do
    time+=($line)   
done &lt;"$file"
</code></pre></li>
<li><p>Cut video between the two black frames.</p>

<pre><code>ffmpeg -i "$video" -ss "${time[0]}" -to "${time[1]}" "$dest"/middle-"$filename"."$extension"
</code></pre></li>
<li><p>Delete no longer needed text file and end.</p>

<pre><code>rm chapter.txt
fi
done
</code></pre></li>
</ol>

CC BY-SA 4.0
2018-07-11T17:17:04.823Z
Hmm... I'm getting a "mkdir: /output: Permission denied
mkdir: /output: No such file or directory" error. (I will try using my brother - who knows the command line well - to get past this part.)
Custom Soundtrack
CC BY-SA 4.0
2018-07-12T01:11:58.000Z
Since it appears to be a permissions issue, try running it under Sudo
aclab28
CC BY-SA 4.0
2018-07-10T13:19:50.743Z
2018-07-10T20:03:05.877Z
2018-07-10T20:03:05.877Z
aclab28
<p>I have many hundreds of videos, each a few minutes long and to which was appended an intro and outro sequence that I would like to automatically remove. There is always an all-black frame immediately following the intro sequence and immediately preceding the outro sequence.</p>

<p>Is there a way to batch (for all videos in a folder, macOS High Sierra) detect and remove all the material preceding the first black frame in a video? (I have the same question for removing the outro sequence, if a workable solution exists.)</p>

<p>Thank you.</p>

<video><ffmpeg>
How can I batch remove from videos all material that comes before the first all-black frame?
Custom Soundtrack
# How can I batch remove from videos all material that comes before the first all-black frame?

(Asked by: username_0 on 2018-07-10)

I have many hundreds of videos, each a few minutes long and to which was appended an intro and outro sequence that I would like to automatically remove. There is always an all-black frame immediately following the intro sequence and immediately preceding the outro sequence.

Is there a way to batch (for all videos in a folder, macOS High Sierra) detect and remove all the material preceding the first black frame in a video? (I have the same question for removing the outro sequence, if a workable solution exists.)

Thank you.

**Tags:** video, ffmpeg


---

## 1 answers

### Answer by username_1 on 2018-07-10. Score: 2 | Accepted answer

You can try using ffprobe scene detection tool to find the black frame time, then use ffmpeg to seek and trim.
I'm not very good at scripting, but here is something you can try. It is very important that the black frame be only a single frame for this to work! Also, ffprobe's scene tool is very picky about file paths, so make sure there are no spaces or special characters in the filename or file path of any of the videos you need to edit.
Copy and paste the following code into a bash or command script file. I'll walk through each step to explain what's happening after.
#!/bin/bash

echo "Enter Source Folder"

read input

cd "$input"

mkdir -p "$input"/output

dir="$input"/output

for video in *; do

if [[ -f "$video" ]]; then

filename="${video%.*}"
extension="${video##*.}"

mkdir "$dir"/"$filename"

dest="$dir"/"$filename"

ffprobe -show_frames -of compact=p=0 -f lavfi "movie="$video",select=gt(scene\,0.9)" -show_entries frame=best_effort_timestamp_time > tmp.txt

sed -e 's/.*best_effort_timestamp_time=\(.*\)|tag:lavfi.scene_score.*/\1/' tmp.txt > chapter.txt
rm tmp.txt

file=chapter.txt
while read line
do
    time+=($line)   
done <"$file"

ffmpeg -i "$video" -ss "${time[0]}" -to "${time[1]}" "$dest"/middle-"$filename"."$extension"

rm chapter.txt

fi

done


Breakdown
Determine the folder that contains the videos you want to trim and set it as the working directory.
#!/bin/bash
echo "Enter Source Folder"
read input
cd "$input"

Make output directory
mkdir -p "$input"/output
dir="$input"/output

For each file in the folder do this:

make sure it's a file and not a folder 
save the filename and extension as a variable
create a new output directory for each video
for video in *; do
if [[ -f "$video" ]]; then
filename="${video%.*}"
extension="${video##*.}"
mkdir "$dir"/"$filename"
dest="$dir"/"$filename"

Find any frames that are 9/10 different than the frame before and save the time to a txt file.
ffprobe -show_frames -of compact=p=0 -f lavfi "movie="$video",select=gt(scene\,0.9)" -show_entries frame=best_effort_timestamp_time > tmp.txt

Clean up and replace text file.
sed -e 's/.*best_effort_timestamp_time=\(.*\)|tag:lavfi.scene_score.*/\1/' tmp.txt > chapter.txt
rm tmp.txt

Create array variable from the text file times.
file=chapter.txt
while read line
do
    time+=($line)   
done <"$file"

Cut video between the two black frames.
ffmpeg -i "$video" -ss "${time[0]}" -to "${time[1]}" "$dest"/middle-"$filename"."$extension"

Delete no longer needed text file and end.
rm chapter.txt
fi
done

> **Comments:**
> **username_0**: Hmm... I'm getting a "mkdir: /output: Permission denied mkdir: /output: No such file or directory" error. (I will try using my brother - who knows the command line well - to get past this part.)  
> **username_1**: Since it appears to be a permissions issue, try running it under Sudo  

---

