LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2018, 09:05 AM   #1
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Rep: Reputation: Disabled
Cool Extract JPGs from Nested Directory of MP4s, Leaving Each JPG in its Source MP4 Directory


I have this script, which creates a directory for each file, & moves the file into the directory.

Code:
#!/bin/bash
for file in *; do
  if [[ -f "$file" ]]; then
    mkdir "${file%.*}"
    mv "$file" "${file%.*}"
  fi
done
Code:
Now, I need to 'visit' each file (all MP4s) inside its directory, 
    then create a JPG from each frame with ffmpeg, 
    leaving JPGs inside that directory.
I create JPGs from MP4's with ..
Code:
ffmpeg -i putin.mp4 -y -f image2 -c:v mjpeg %03d.jpg
[using video "putin.mp4"]

I don't know how to ensure the extracted JPGs appear inside their associated MP4's directory. If I apply the ffmpeg extraction in the foregoing, JPGs will go in a higher-level directory, I think.
 
Old 06-09-2018, 09:19 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I'm not an expert at Bash (which may illustrate itself with my answer), but can you not just cd into each directory before issuing the ffmpeg command?

Or, as you are probably looping through the directories with a variable anyway, use that variable to provide a path for ffmpeg's -i option and the final .jpg option (if the latter is allowed)?
 
Old 06-09-2018, 10:31 AM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
You can use a command substitution with 'pwd', like
Code:
ffmpeg -i file $(pwd)/output_file
 
Old 06-09-2018, 10:46 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
To elaborate on the other posts but untested.

Code:
  if [[ -f "$file" ]]; then
    newdir="${file%.*}"
    mkdir "$newdir"
    mv "$file" "$newdir"
    ffmpeg -i "$newdir"/"$file" -y -f image2 -c:v mjpeg "$newdir"/%03d.jpg
  fi
 
Old 06-09-2018, 10:51 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209
Quote:
Originally Posted by AwesomeMachine View Post
You can use a command substitution with 'pwd', like
Code:
ffmpeg -i file $(pwd)/output_file
While that's true, how is
Code:
$(pwd)
different from
Code:
./
?
Doesn't pwd always return the name of the current directory?
 
Old 06-09-2018, 11:39 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
#!/bin/bash
for file in *; do
  if [[ -f "$file" ]]; then
    mkdir "${file%.*}"
    mv "$file" "${file%.*}"
  fi
done
that is set up to run and create a new sub-dir within that parent dir. If when running this and it does not complete in time, say you got a lot to do and x amount of time to do them, and you do not complete the task, then you go back to finish it, what effects are you going to experience?
Code:
#!/bin/bash
working_dir= 
move_to= 
script_dir= 
for file in "$working_dir"/* ; 
do
  if [[ -f "$file" ]]; then
	source=${file%.*} 
	moved=${source/$working_dir/$move_to}
	mkdir -pv "$moved"
	mv "$file" "$moved"   #frames per seconds = every 30 seconds
	#ffmpeg -i "$moved"/"${file##*/}" -vf fps=1/30 "$moved"/"${file##*/}"-%04d.jpg
	#or
	ffmpeg -i "$moved"/"${file##*/}" -vf fps=1 "$moved"/"${file##*/}"-%04d.jpg
  fi
 done
Tested:

strips off what it needs to in order to look where they are at, and put it where you want it, then created multi frames attaching the filename to the number, so when one looks at the image file they will know what or where it came from, and is pertaining to.

mod:
Code:
image2
is for making movies out of images.

https://ffmpeg.org/ffmpeg-formats.html#Examples-1

Last edited by BW-userx; 06-09-2018 at 01:39 PM.
 
Old 06-09-2018, 12:40 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
#5, I thought he just wanted the output put in the working directory.
 
Old 06-09-2018, 12:42 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
...
 
Old 06-10-2018, 01:23 PM   #9
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
thanks to all who have offered suggestions. I will be trying them out and reporting back in short order.
 
Old 06-13-2018, 10:07 AM   #10
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
Implemented Suggestions. Frame to JPG Convert Stopped Early

Thanks to all for great suggestions.

Quote:
Originally Posted by BW-userx View Post
[code]
Code:
#!/bin/bash
working_dir=/media/data/testMovies
move_to=/media/data/rdThumbNailed
script_dir=/home/userx/scripts
for file in "$working_dir"/* ;
do
  if [[ -f "$file" ]]; then
    source=${file%.*}
    move_to=${source/$working_dir/$move_to}
    mkdir -pv "$move_to"
    mv "$file" "$move_to"
  ffmpeg -i "$move_to"/"${file##*/}" -y -f image2 -c:v mjpeg
"$move_to"/"${file##*/}"-%03d.jpg
  fi
done
I pasted the foregoing code at command prompt.
This created the first 9870 JPGs from the first video,
stopping way early near 10,000th frame .. hmmm.

Below is edited terminal output -- two errors highlighted, as follows ..
1] "Output file #0 does not contain any stream." -- AND --
2] "[adp @ 0xfb8400] Format adp detected only with low score of 25,
misdetection possible!"

I changed -%03d.jpg to -%05d.jpg, for more JPG names. Didn't help.

Any suggestions why it's stopping after 9870th frame much appreciated !!
Thanks in advance.

[ *** BEGIN TERMINAL OUTPUT *** ]

9790 fps= 37 q=24.8 size=N/A time=00:02:43.32 bitrate=N/A dup=2 drop=0 frame= 9810 fps= 37 q=24.8 size=N/A
[...SNIP...]
frame= 9870 fps= 37 q=24.8 Lsize=N/A time=00:02:44.66 bitrate=N/A dup=2 drop=0
video:535625kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[...SNIP...]


[ *** ERROR MESSAGE *** ]
Output file #0 does not contain any stream


mkdir: created directory '/media/data/rdThumbNailed/MVI_0001/MVI_0004/MVI_0008-j'
ffmpeg version 2.8.14-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu
[...SNIP...]
--enable-libass --enable-libbluray --enable-libbs2b
[...SNIP...]
-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
WARNING: library configuration mismatch
avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared
[...SNIP...]
enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libvo_aacenc --enable-libvo_amrwbenc
libavutil 54. 31.100 / 54. 31.100
[...SNIP...]
libpostproc 53. 3.100 / 53. 3.100


[ *** ERROR MESSAGE *** ]
[adp @ 0xfb8400] Format adp detected only with low score of 25, misdetection possible!


Input #0, adp, from '/media/data/rdThumbNailed/MVI_0001/MVI_0004/MVI_0008-j/MVI_0008-j.MP4':
Duration: 02:40:42.12, start: 0.000000, bitrate: 438 kb/s
Stream #0:0: Audio: adpcm_dtk, 48000 Hz, stereo, s16p
Output #0, image2, to '/media/data/rdThumbNailed/MVI_0001/MVI_0004/MVI_0008-j/MVI_0008-j.MP4-%05d.jpg':


[ *** ERROR MESSAGE *** ]
Output file #0 does not contain any stream


eric-HP-Compaq-dc7900-Small-Form-Factor eric # for file in "$working_dir"/* ; do if [[ -f "$file" ]]; then source=${file%.*} ; move_to=${source/$working_dir/$move_to}; mkdir -pv "$move_to"; mv "$file" "$move_to"; ffmpeg -i "$move_to"/"${file##*/}" -y -f image2 -c:v mjpeg "$move_to"/"${file##*/}"-%05d.jpg; fi; done
 
Old 06-13-2018, 11:45 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ericlindellnyc View Post
Thanks to all for great suggestions.

I pasted the foregoing code at command prompt.
This created the first 9870 JPGs from the first video,
stopping way early near 10,000th frame .. hmmm.

Below is edited terminal output -- two errors highlighted, as follows ..
1] "Output file #0 does not contain any stream." -- AND --
2] "[adp @ 0xfb8400] Format adp detected only with low score of 25,
misdetection possible!"

I changed -%03d.jpg to -%05d.jpg, for more JPG names. Didn't help.

Any suggestions why it's stopping after 9870th frame much appreciated !!
Thanks in advance.
top of head guess, if it is doing this with just one the same one, bad file. if that is a true, then try re-sampling the movie using perhaps handbrake to fix it, maybe, hopefully. anything other than that I'd have to research it. something you can do.

https://ffmpeg.org/ffmpeg.html

search for limitations on that page. it says something about stream picking. I didn't read it all.

Last edited by BW-userx; 06-13-2018 at 11:48 AM.
 
Old 06-13-2018, 03:43 PM   #12
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
So here's a few things I tried since last time ..

I tried different video files. No difference. This has worked before with MP4s.
I tried adding this switch: "-err_detect ignore_err" -- still no difference.
I checked -ss option, which someone recommended. Doesn't seem to apply.
"-map 1:a" -- possibly relevant. Don't quite understand it yet.

Checked the ffmpeg documentation page you linked. Found this, re specify codec -- MP4?

Stream handling is independent of stream selection, with an exception for subtitles described below. Stream handling is set via the -codec option addressed to streams within a specific output file. In particular, codec options are applied by ffmpeg after the stream selection process and thus do not influence the latter. If no -codec option is specified for a stream type, ffmpeg will select the default encoder registered by the output file muxer.

also looking into VLC -- which can output frames from a video. But can it do so in batch for multiple videos? Still checking

Thanks again for your helpful insights.
 
Old 06-13-2018, 03:51 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ericlindellnyc View Post
So here's a few things I tried since last time ..

I tried different video files. No difference. This has worked before with MP4s.
I tried adding this switch: "-err_detect ignore_err" -- still no difference.
I checked -ss option, which someone recommended. Doesn't seem to apply.
"-map 1:a" -- possibly relevant. Don't quite understand it yet.

Checked the ffmpeg documentation page you linked. Found this, re specify codec -- MP4?

Stream handling is independent of stream selection, with an exception for subtitles described below. Stream handling is set via the -codec option addressed to streams within a specific output file. In particular, codec options are applied by ffmpeg after the stream selection process and thus do not influence the latter. If no -codec option is specified for a stream type, ffmpeg will select the default encoder registered by the output file muxer.

also looking into VLC -- which can output frames from a video. But can it do so in batch for multiple videos? Still checking

Thanks again for your helpful insights.
how big/hours, minutes is your file, and what format is it?
maybe I'll find something just as long and make sure its the same format and mess about with it to see what I see.
 
Old 06-13-2018, 06:14 PM   #14
ericlindellnyc
Member
 
Registered: Jun 2017
Posts: 181

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
how big/hours, minutes is your file, and what format is it? maybe I'll find something just as long and make sure its the same format and mess about with it to see what I see.
  • The files are almost all MP4s, with a few 3GPs.
  • They range from a few megabytes to 4 gb.
  • Duration from under a minute to about 15 minutes.

Please bear in mind that in OP, I gave the ffmpeg command that has been working successfully when run on a single file. What I've been trying to do in this thread is obtain the same functionality processing videos in batch.

Code:
ffmpeg -i putin.mp4 -y -f image2 -c:v mjpeg %03d.jpg
Thank you again for your kind assistance.
 
Old 06-13-2018, 06:46 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ericlindellnyc View Post
  • The files are almost all MP4s, with a few 3GPs.
  • They range from a few megabytes to 4 gb.
  • Duration from under a minute to about 15 minutes.

Please bear in mind that in OP, I gave the ffmpeg command that has been working successfully when run on a single file. What I've been trying to do in this thread is obtain the same functionality processing videos in batch.

Code:
ffmpeg -i putin.mp4 -y -f image2 -c:v mjpeg %03d.jpg
Thank you again for your kind assistance.
just plug this in using your old stuff
Code:
ffmpeg -i "$moved"/"${file##*/}" -y -f image2 -c:v mjpeg "$moved"/"${file##*/}"-%07d.jpg #changed the number for zeros placements
it's getting me past mp4-0140657.jpg' and still going, adding -%07d.jpg - in case you didn't notice it in the code block.

Last edited by BW-userx; 06-13-2018 at 07:15 PM.
 
  


Reply

Tags
ffmpeg, jpg


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shorten long paths in deeply nested directory structure ericlindellnyc Linux - Newbie 25 09-23-2022 12:00 AM
[SOLVED] Deleting the source directory and build directory after compiling each package ganiz Linux From Scratch 5 01-11-2018 02:02 AM
[SOLVED] ls: cannot access *.{jpg,png}: No such file or directory angel'le Programming 5 09-28-2013 09:21 PM
on make -j 2.. executing default commands.prints..leaving directory Error 2 manoj7410 Linux - Distributions 5 06-15-2012 11:08 PM
[SOLVED] Need to copy all .jpg images in subdirectories to a specific directory sikanders Linux - Newbie 5 08-06-2010 07:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration